我在任何地方都没有找到我的问题的答案,而且我对 javascript 一无所知,所以我自己无法弄清楚。
如果我构建了 jQuery 移动网站,每个页面都在单独的 html 文件中 ( single page template )。我可以使用标准异步 Google Analytics 代码吗,还是必须进行类似于 those used in multi page template 的修改?
如果有人能回答这个问题,将非常感激。
最佳答案
是的,您可以使用标准的 Google Analytics(分析)代码。然而,由于 jQuery Mobile 处理页面导航的方式,您需要将某些页面 View “推送”到 Google Analytics。
例如,如果您的网站上有一个位于 contact.html
的联系表单,一旦提交,就会转到 process.php
页面,然后在完成后,用户到达 thank-you.html
,您将需要调用一些 JavaScript 将综合浏览量“推送”到 Google Analytics。
例如,如果您的 jQuery Mobile 页面元素 (data-role="page"
) 具有 id="thank-you"
,那么我会使用此元素:
<script type="text/javascript">
$(document).delegate('#thank-you', 'pageshow', function () {
//Your code for each thank you page load here
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview', '/thank-you.html']);
});
</script>
更新:
我会将其放入您的 script.js
文件中,该文件在加载 jQuery 和 jQuery Mobile 后包含在头部中。这会在每个 data-role="page"
pageshow
事件上触发,并且目前在我的实时项目中运行得很好。
$('[data-role=page]').live('pageshow', function (event, ui) {
try {
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
hash = location.hash;
if (hash) {
_gaq.push(['_trackPageview', hash.substr(1)]);
} else {
_gaq.push(['_trackPageview']);
}
} catch(err) {
}
});
关于jquery-mobile - jQuery mobile 和 Google Analytics - 单页模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13131488/