Google Analytics API in Javascript

不问归期 提交于 2020-01-02 07:08:32

问题


I am using the Google Analytics Javascript library to let users view a GeoMap of the particular page they are on. However, everytime they attempt to do so, thye have to go through an authentication process only to have my data displayed on my page. How can I find an alternative to this. I only want to embed my Analytics data through a visualized graph on my page so that all anonymous viewers can see it


**We have created an account in Google analytics. Now we are able to get the statistics of the site. Open the site http://Example.com Click on Authorize Analytics button. Provide the credentials of the Google account in the opened window. Username: xxxx@gmail.com Password: xxx It will redirect to http://Example.com page. Enter the table id ga:61737784 and click on Draw Charts Button, then u will get the statistics for the site.

My requirement is without login in to Google analytics site I should get the statistics. Because the end user don’t know the credentials of Google analytics. The below is the html and javascript code.**

  <script src="http://apis.google.com/js/client.js?onload=gadashInit" type="text/javascript"></script>
    <script src="http://analytics-api-samples.googlecode.com/svn/trunk/src/reporting/javascript/ez-ga-dash/gadash-1.0.js" type="text/javascript"></script>
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>

    <script type="text/javascript">
      gadash.configKeys({
        'apiKey': xxxxx',
        'clientId':xxxxx'
      }); 
      var dataOverTime = new gadash.Chart();
      var scoreCard = new gadash.Chart();
      var sourceMediumTable = new gadash.Chart(); 
      var baseConfig = {
        'last-n-days': 30,
        'query': {
          'metrics': 'ga:visitors, ga:visits, ga:pageviews',
        },
        'chartOptions': {
          width: 400
        }
      }; 
      var dataOverTimeConfig = {
        'divContainer': 'dataOverTimeConfig',
        'type': 'LineChart',
        'query': {
          'dimensions': 'ga:date',
          'sort': 'ga:date'
        },
        'chartOptions': {
          height: 250,
          legend: {position: 'bottom'},
          hAxis: {title:'Date'},
          curveType: 'function'
        }
      }; 
      var scoreCardConfig = {
        'divContainer': 'scorecard',
      }; 
      var sourceMediumTableConfig = {
        'divContainer': 'sourceMediumTableConfig',
        'query': {
          'dimensions': 'ga:source,ga:medium',
          'sort': '-ga:visitors',
          'max-results': 100
        }
      };
      function renderGraph() {

         baseConfig.query.ids = document.getElementById('tableId').value;

         dataOverTime.set(baseConfig).set(dataOverTimeConfig).render();

         scoreCard.set(baseConfig).set(scoreCardConfig).render();
    sourceMediumTable.set(baseConfig).set(sourceMediumTableConfig).render();   

         document.getElementById('ui').style.display = 'block';

    return false;
       }
        </script>
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-33166458-1']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-32934024-1']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google.com/jsapi';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        <script type="text/javascript">

          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-32934024-1']);
          _gaq.push(['_trackPageview']);

          (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'analytics-api-samples.googlecode.com/svn/trunk/src/reporting/javascript/ez-ga-dash/gadash-1.0.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
          })();

        </script>
        <script type="text/javascript">

          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-32934024-1']);
          _gaq.push(['_trackPageview']);

          (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'apis.google.com/js/client.js?onload=gadashInit';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
          })();

        </script>
        <button id="Button1" style="visibility: hidden;">
        Authorize Analytics</button>
        <hr />
        <p>
        Enter your Table ID:
        <input type="text" id="tableId" />
        <button onclick="return renderGraph()">
        Draw Charts</button></p>
        <hr />
        <div id="ui" style="display: none;">
        <h4>
        Data Over Time</h4>
        <div id="dataOverTimeConfig">
        </div>
        <h5>
        Totals for each metric</h5>
        <div id="scorecard">
        </div>
        <h5>
        Source and Mediums by Visitor</h5>
        <div id="sourceMediumTableConfig">
        </div>

回答1:


You can use Google Analytics superProxy to share data with users who are not authenticated.
You can create queries to Analytics with superProxy and then use the data stored with superProxy to construct your charts. I ran into the same problem, and this was the only solution I could find.

https://developers.google.com/analytics/solutions/google-analytics-super-proxy




回答2:


Can you share more information about how you access Google Analytics?

Check out http://code.google.com/p/google-api-javascript-client/wiki/Authentication on how to set authentication. Note that your credentials are visible in JavaScript, so make sure you create a user with just the minimal permissions you need



来源:https://stackoverflow.com/questions/11373797/google-analytics-api-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!