我尝试使用多页面模板开发jQuery Mobile应用程序。我想将Google Analytics添加到应用程序中。
我参考了这些文件Using Google Analytics with jQuery Mobile。它似乎在我的应用程序中不起作用。
我的应用中的代码如下:
<script type="text/javascript">
var _gaq = _gaq || [];
(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);
})();
$('[data-role=page]').on('pageshow', function (event, ui) {
try {
_gaq.push(['_setAccount', 'UA-30119852-1']);
hash = location.hash;
if (hash) {
_gaq.push(['_trackPageview', hash.substr(1)]);
} else {
_gaq.push(['_trackPageview']);
}
} catch(err) {
}
});
</script>
如何将Google Analytics与jQuery Mobile应用程序集成?
答案 0 :(得分:0)
$(document).delegate('[data-role=page]', 'pageshow', function (event, ui) {
console.log('pageshow event handler called for ');
var url = location.href;
console.log('pageshow event url = '+url);
try
{
//_gaq.push(['_trackPageview', url]);
_gaq.push( ['_trackPageview', event.target.id] );
console.log('done pushing page '+url);
}
catch(error) {
// error catch
console.log('got error '+error);
}
});