Membuat Dashboard Google Analitycs Dengan Google API

Untuk mengakses layanan google analitycs tanpa perlu repot-repot buka google.com/analytics/ bisa kita lakukan dengan membuat simple dashboard di web kita sendiri. Langkah-langkahnya cukup simple. Buat aja file yang akan kita gunakan sebagai halaman dashboard.

analitycsLangkah pertama : Load the Embed API library

<script>
(function(w,d,s,g,js,fs){
  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
  js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
  js.src='https://apis.google.com/js/platform.js';
  fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>

Langkah kedua : Buat <div> untuk menampung komponen dashboard.

<div id="embed-api-auth-container"></div>
<div id="chart-container"></div>
<div id="view-selector-container"></div>

Langkah ketiga : tambahkan dashboard code (Client ID dapat dibuat dengan mengikuti langkahnya).

<script>

gapi.analytics.ready(function() {

  /**
   * Authorize the user immediately if the user has already granted access.
   * If no access has been created, render an authorize button inside the
   * element with the ID "embed-api-auth-container".
   */
  gapi.analytics.auth.authorize({
    container: 'embed-api-auth-container',
    clientid: 'REPLACE WITH YOUR CLIENT ID'
  });


  /**
   * Create a new ViewSelector instance to be rendered inside of an
   * element with the id "view-selector-container".
   */
  var viewSelector = new gapi.analytics.ViewSelector({
    container: 'view-selector-container'
  });

  // Render the view selector to the page.
  viewSelector.execute();


  /**
   * Create a new DataChart instance with the given query parameters
   * and Google chart options. It will be rendered inside an element
   * with the id "chart-container".
   */
  var dataChart = new gapi.analytics.googleCharts.DataChart({
    query: {
      metrics: 'ga:sessions',
      dimensions: 'ga:date',
      'start-date': '30daysAgo',
      'end-date': 'yesterday'
    },
    chart: {
      container: 'chart-container',
      type: 'LINE',
      options: {
        width: '100%'
      }
    }
  });


  /**
   * Render the dataChart on the page whenever a new view is selected.
   */
  viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: ids}}).execute();
  });

});
</script>

Untuk membuat dashboard google analitycs yang lebih komplek bisa mencoba langkah-langkahnya di ga-dev-tools.appspot.com/embed-api/
Selamat mencoba dan semoga bermanfaat. 😀

Tinggalkan Balasan

This site uses Akismet to reduce spam. Learn how your comment data is processed.