Google analytics API - Tracking subdomains

十年热恋 提交于 2019-12-07 17:33:41

问题


I have a website where each user registers as subdomain (username.domain.com). I'm tracking all subdomains with the folllowing code:

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXX-X']);
  _gaq.push(['_setDomainName', '.domain.com']);
  _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);
  })();

I want to show custom statistics for each user using the GA API. Since i'm using only one GA account for tracking all "subdomains", how can i filter the data in order to show only the data for each user (subdomain)?

Thanks.


回答1:


setDomainName is only relevant for the cookies that Google Analytics uses for tracking sessions. That is, setting it as you are makes it such that all traffic across those subdomains will be tracked consistently.

However, that doesn't change the domain data that gets sent to Google Analytics. All hits are sent to Google Analytics with the hostname (document.domain in JavaScript) of the pageview. That value is accessible in the API under the dimension ga:hostname. You can either use it as a dimension for a query, or use it as a filter for data (ie, say "only return the data I'm querying for this specific filter.)

So, regardless of the setDomainName configuration, you'll be able to access and segment on a subdomain level basis using the Google Analytics API.



来源:https://stackoverflow.com/questions/7033218/google-analytics-api-tracking-subdomains

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