简体   繁体   English

针对不同环境分离Google Analytics(分析)

[英]Separating Google Analytics For Different Environments

As with most web applications, I have 3 different environments – Dev, QA and Production. 与大多数Web应用程序一样,我有3个不同的环境-开发,质量检查和生产。

I have 3 Google Analytics profile-ID's. 我有3个Google Analytics(分析)配置文件ID。

I'm looking to understand the most efficient way of the application understanding what environment it is in, and using the corresponding ID without me having to update the 'UA-XXXXX-X' during every promotion. 我希望了解应用程序的最有效方法,以了解应用程序所处的环境,并使用相应的ID,而无需在每次促销期间都更新“ UA-XXXXX-X”。

Each environment has its own sub-domain, for example: 每个环境都有其自己的子域,例如:

Production: www.MyWebsite.com QA: www.qa.MyWebsite.com Dev: www.dev.MyWebsite.com 生产:www.MyWebsite.com质量检查:www.qa.MyWebsite.com开发人员:www.dev.MyWebsite.com

I was thinking to use jQuery to capture the hostname, and use the relevant ID. 我当时在考虑使用jQuery捕获主机名,并使用相关的ID。 My application is created using HTML5, jQuery and CSS. 我的应用程序是使用HTML5,jQuery和CSS创建的。

For example: 例如:

var hostname = window.location.hostname;
var profileId;

If hostname=’qa.MyWebsite.com’
{
    profileId = ‘123qaId’
}
Elseif hostname=’dev.MyWebsite.com’
{
    profileId = ‘123devId’
}

Thoughts? 思考?

var envKey = {
    'dev.mydomain.com': 'XX-XXXXXXXX-X',
    'qa.mydomain.com': 'YY-YYYYYYYY-Y',
    'mydomain.com': 'ZZ-ZZZZZZZZ-Z'
};
var hostname = window.location.hostname;

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', envKey[hostname]]);
  _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);
  })();
    var envKey = {
    'mydomain.com/product1': 'XX-XXXXXXXX-X',
    'mydomain.com/product2': 'YY-YYYYYYYY-Y',
    'mydomain.com/product2': 'ZZ-ZZZZZZZZ-Z'
};
var hostname = window.location.hostname + window.location.pathname.replace(/(\/[^\/]+)\/.*/, "$1");


var _gaq = _gaq || [];
_gaq.push(['_setAccount', envKey[hostname]]);
_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);
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM