I´m using universal analytics and i´m not sure if this is the right way.
I have a site where the content is loaded and replaced by ajax. The site is developed in MVC (codeigniter). In the head of the static pages i set:
ga('create', 'my-tracking-id', 'auto');
ga('send', {
'hitType': 'pageview',
'dimension1': 'www.myexamplesite.com/test',
'dimension2':'Static page',
'dimension3':'index section'
});
This one work fine but only in the of the static pages. I used the same code at .done() in the ajax request for the dynamic content but google analytics don´t show any data of this content.
What is wrong?
Actual ajax code
$.ajax({
type: "POST",
url: content_url+complement,
dataType: "html",
beforeSend: function() {
//show a loader gif
}
})
.done(function(respuesta){
ga('create', 'my-tracking-id', 'auto');
ga('send', {
'hitType': 'pageview',
'dimension1': 'www.myexamplesite.com/test',
'dimension2':'Static page',
'dimension3':'index section'
});
})
.fail(function( jqXHR, textStatus ){
alert( "Request failed: " + textStatus );
});
Well, you definitely don't need to run the ga('create', ...)
command a second time (that only needs to be done on the initial page load), but that shouldn't actually prevent the hits from being sent. What you have looks fine to me, perhaps you have a JavaScript error or something else preventing the script from executing.
I'd open up the web inspector and do some standard debugging to see what's going on.
Also, the best way to debug analytics.js issues is to load the debug version of the library, then you can see what's actually running and what's not. You shouldn't have to be waiting for hits to come in on the Google Analytics website to debug these types of issues.
https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#debug