I installed some custom tags for the Google Tag Manager on the website to track some user data, but this is not tracking. The dataLayer variable matches multiple data, like this, in the header above the tag manager code.
The process is that the user lands on the page and clicks, it receives data from Wordpress, etc. using localized data and writes this information through an ajax call. We also wanted to register this data with Google Analytics using the tag manager before calling ajax.
Here is the javascript.
jQuery(document).ready(function($){
console.log(gg_data_object);
var MyObject = {
load: function(){
MyObject._do_ajax('landed', null, null);
},
_readCookie: function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return 0;
},
_click_trace: function(event){
MyObject._do_ajax('clicked', this.href, $(this).attr('class'));
},
_submit_trace: function(event){
gg_data_object.user_id = MyObject._readCookie('user_id');
MyObject._do_ajax('submission', gg_data_object.post_type, $(this).attr('class'));
},
_do_ajax: function(event_action, href, class_ref){
var data = {
'action' : 'track_event',
'security' : gg_data_object.ajax_nonce,
'event_action' : event_action,
'current_url' : gg_data_object.current_url,
'date' : gg_data_object.date_today,
'phpsessionid' : gg_data_object.phpsessionid,
'post_id' : gg_data_object.post_id,
'post_type' : gg_data_object.post_type,
'referrer' : gg_data_object.referrer,
'unique_id' : gg_data_object.unique_id,
'user_id' : gg_data_object.user_id,
'clickedon' : href,
'class_location_type' : class_ref
};
dataLayer.push(data);
dataLayer.push({'gg_event_action': event_action});
jQuery.ajax({
type : "post",
dataType : "json",
url : gg_data_object.ajaxurl,
data : data,
success: function(response) {
console.log(response);
return true;
}
});
},
}
MyObject.load();
$(document).on('click', 'a', MyObject._click_trace);
$(document).on('click', 'input[type="submit"]', MyObject._submit_trace);
});
, , , , , , , .
:
- Google Universal Analytics
- : gg_event_type
- DataLayer
Google Analytics.
, , .