0

我有一个域,我们称之为 myhost.com,我在 myhost.com 上有一个脚本 addGAcode.js。该代码包含函数 includeGA(),它为我的客户域的帐户集调用标准 GA 代码。它看起来像这样:

function includeGA() {   
var _gaq = _gaq || [];  
_gaq.push(['_setAccount', 'UA-432432432-43']);  
_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);   })(); 
}

includeGA();

让我们将此域称为groceryinbelfast.com。

现在我添加到groceryofbelfast 脚本标签的meta 以包含myhost.com/addGAcode.js。不幸的是,GA 不起作用。GA 代码已正确加载,但我想是因为它位于其他域上,因此域groceryofbelfast 它无法在groceryofbelfast 域上设置统计数据工作所必需的cookie。

我的概念的重点是在我的服务器上安装 GA 脚本,这样当 Google 更改其中的某些内容或我想要对其进行一些调整时,我不需要每次都打电话给我的客户的网站管理员进行适当的更改。

有任何想法吗?

4

1 回答 1

1

您的includeGA()函数可能无法工作,因为_gaq需要将其声明为全局变量,而不是includeGA()函数内的局部变量。

您可以通过更改为以下内容来解决该特定问题:

function includeGA() {   
    window._gaq = window._gaq || [];  
    _gaq.push(['_setAccount', 'UA-432432432-43']);  
    _gaq.push(['_trackPageview']);

    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);
}
于 2012-10-10T15:08:04.370 回答