-3

有人可以告诉我为什么分析代码没有加载吗?对于美国游客,正在显示警报。

<script type="text/javascript">

$.getScript('http://www.geoplugin.net/javascript.gp', function() {
    $location =  geoplugin_countryName();

    if($location == "Canada" || $location == "United States") {
      alert("12");

    } else {

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-9240923-27']);
      _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>
4

3 回答 3

1

刚刚在 JSFiddle 上测试了这个片段:http: //jsfiddle.net/wDzBc/

$.getScript('http://www.geoplugin.net/javascript.gp', function() {
    var location =  geoplugin_countryName();

    if(location == "Canada" || location == "United States") {
      alert("12");
    } else {
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-9240923-27']);
      _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);
        alert('ga');
      })();          
    }
});
于 2012-07-12T14:57:56.987 回答
1

如果您希望分析仅适用于加拿大和美国,只需删除该 } else {声明,如评论中所述

编辑:好的然后:

$.getScript('http://www.geoplugin.net/javascript.gp', function() {
    $location =  geoplugin_countryName();

    if($location == "Canada" || $location == "United States") {
    // HERE INSERT THE CODE FOR LOADING ANALYTICS FOR USA
    } else {
        // HERE INSERT THE CODE FOR LOADING ANALYTICS FOR OTHER COUNTRY
      }          
    });
于 2012-07-12T14:55:17.050 回答
1

由于您似乎想根据位置更改帐户,因此以下代码应该是您想要的:

$.getScript('http://www.geoplugin.net/javascript.gp', function() {
    var $location =  geoplugin_countryName();
    var account = "";

    if($location == "Canada" || $location == "United States")
        account = 'UA-9240923-27'
    else
        account = "otherAccount";

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

编辑

您的 javascript 末尾有一个非法字符。这是我查看源代码时脚本末尾的样子:

});​

您还缺少type="text/javascripton 您的第一个脚本标记,您应该使用 doctype、html、head 和 body 正确格式化您的 html 文件。

于 2012-07-12T14:52:35.787 回答