javascript - 无法让 google 分析在我的页面上运行,该页面使用 AJAX 加载其页面

标签 javascript jquery html ajax

经过一番头痛之后,我让我的网站全部通过 ajax 加载其页面。原因是客户希望它能够像他们的旧 Flash 网站一样运行......呃。

无论如何,一切都很顺利,但后来我添加了谷歌分析跟踪器,它就不再加载了!

这就是我如何进行ajax:

$("li.home a").click (function() {       
    ajax_load("index.php",this);
    return false;
});
$("li.location a").click (function() {
    ajax_load("location.php",this);
    return false;
});

etc...

function ajax_load(page,element) {

    // deals with making an element active in left nav
    $('#mainnav').find('li.active').removeClass('active');
    $(element).parent().addClass('active');

    // empty the wrapper, then add new content with a fade effect
    $("#wrapper").empty();
    $("#wrapper").css({'display' : 'none'})
    $.ajax({
        type: "POST",
        url: page,
        success: function(msg){            
            $("#wrapper").append(msg);
            $("#wrapper").fadeIn();
        }
    });

    // set the page hash, so we can add these pages to favorites if wanted
    var strLen = page.length;
    page = page.slice(0,strLen-4);
    window.location.hash = page;
}

所有单独的页面都是 php 文件.. 当它们加载时,会在文件的开头进行检查以查看其是否是 ajax 请求。如果是......它会跳过标题和导航到内容,然后跳过页脚。这使得它可以在禁用 JavaScript 时工作。

因此,为了让 google 分析能够处理 ajax 页面加载,我想我可以简单地将 google 跟踪代码粘贴在 ajax 请求时加载的区域内...

好吧,这不起作用!目前,唯一带有谷歌分析跟踪代码的页面是平面图页面,因此您可以看到正在发生的情况。任何指点将不胜感激!

这是我使用的谷歌跟踪代码:

    <script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-11737385-5");
pageTracker._trackPageview();
} catch(err) {}</script>

//更新//

我相信这应该可以,谢谢佩卡!

    $.ajax({
    type: "POST",
    url: page,
    success: function(msg){            
        $("#wrapper").append(msg);
        $("#wrapper").fadeIn();
        pageTracker._trackPageview(page);
    }

最佳答案

您可以使用一个函数,_trackPageview。请参阅Google Analytics manual :

How do I track AJAX applications?

With a typical HTML page, you can use the URL to differentiate between multiple pageviews. But in an AJAX application, a request to the server is made without changing the URL of the page, making it difficult to track.

By calling the _trackPageview function, however, you can assign a page filename to any AJAX event. Typically, this is done as part of the onreadystatechange function, after the data has been returned and all updates to the page have been made.

关于javascript - 无法让 google 分析在我的页面上运行,该页面使用 AJAX 加载其页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2067592/

相关文章:

javascript - 如何在javascript中更改标签颜色

javascript - 仅使用 CSS 更改按钮单击的背景?

javascript - 组件更改后,带有外部 JavaScript 的 Angular 不会运行

javascript - 如何从网页中的嵌入式pdf中获取选定的文本?

html - 如何防止填充属性更改 CSS 中的宽度或高度?

javascript - 如何让div在轮播上滚动 Bootstrap

javascript - 从 API 加载 XML

javascript - 在 javascript 中创建自己的 sort() 函数

javascript - ajax .load 调用后执行 javascript 时遇到问题

javascript - 我怎样才能在这种脚本中将图像居中?