最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

web - Get pageview's from Google Analytics API in Javascript - Stack Overflow

programmeradmin1浏览0评论

I am having trouble with fetching data from Google Analytics API with javascript. I can't seem to fetch anything but lets say its something basic like pageviews.

I am using Analytics.js

This is the code i am using to connect to GA API:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics/analytics.js','ga');

    ga('create', 'XXX', 'example');
    ga('send', 'pageview');

So how do i get the pageviews now? I tried using this code but it returns undefined:

ga(function() {
      var tracker = ga.getByName('t0');
      alert(tracker.get('page'));
    });

I am having trouble with fetching data from Google Analytics API with javascript. I can't seem to fetch anything but lets say its something basic like pageviews.

I am using Analytics.js

This is the code i am using to connect to GA API:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics./analytics.js','ga');

    ga('create', 'XXX', 'example.');
    ga('send', 'pageview');

So how do i get the pageviews now? I tried using this code but it returns undefined:

ga(function() {
      var tracker = ga.getByName('t0');
      alert(tracker.get('page'));
    });
Share Improve this question asked Apr 15, 2013 at 14:48 delokideloki 1,7712 gold badges19 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Analytics.js is a tracking API. It just tracks/sends data to Google Analytics.

In order to retrieve data from Google Analytics you need to use a different API. Look for the Reporting API.

https://developers.google./analytics/devguides/reporting/

You can get variables you send as payload using tracker object.

For example:

ga(function(tracker) {
  var defaultPage = tracker.get('location');console.log(defaultPage);
});

(See analytics_debug.js for more information)

发布评论

评论列表(0)

  1. 暂无评论