first, to be sure the google analytics api is new version, such as
notice 1: ajaxSetup is better writeen within $( document ).ready() to be sure the google analytics api is loaded.
notice 2: the opts.url is the ajax url you write, to record corect pageview, the best way is use fullpath, such as '/folder_name/filename.php'
notice 1: to be sure the google analytics api is loaded
notice2: the beforeSend function will overwrite the beforeSend function of ajaxSetup
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-x']);
_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);
})();
the way 1. use ajaxSetup for all calls to $.ajax.notice 1: ajaxSetup is better writeen within $( document ).ready() to be sure the google analytics api is loaded.
notice 2: the opts.url is the ajax url you write, to record corect pageview, the best way is use fullpath, such as '/folder_name/filename.php'
$(function () {
$.ajaxSetup({
beforeSend: function (xhr, opts) {
//avoid cache of the iOS6 Safari
if (opts.url.indexOf('?') > 0) {
opts.url += '&_t=' + (new Date()).getTime();
} else {
opts.url += '?_t=' + (new Date()).getTime();
}
//google analytics
if (opts.data != undefined) {
if (typeof (_gaq) != 'undefined') {
_gaq.push(['_trackPageview', opts.url + '&' + opts.data]);
}
} else {
if (typeof (_gaq) != 'undefined') {
_gaq.push(['_trackPageview', opts.url]);
}
}
}
});
});
the way 2. setting in every ajax function
notice 1: to be sure the google analytics api is loaded
notice2: the beforeSend function will overwrite the beforeSend function of ajaxSetup
function JobIndexData(p) {
var url = location.href;
var qs = url.substr(url.indexOf('?'));
url='job-index-data.asp' + qs + '&page=' + p;
$.ajax({
url: url,
type: "POST",
success: function (data) {
$('#recordMore_' + p).parent().append(data);
$('#recordMore_' + p).remove();
},
beforeSend: function () {
if (typeof (_gaq) != 'undefined') {
_gaq.push(['_trackPageview', '/mobileweb/' + url]);
}
if (p > 1) {
$('#recordMore_' + p).html('loading...');
}
}
});
}
留言
張貼留言