I set up some custom tags for Google Tag manager on a website to track some custom data, but it is not tracking. The dataLayer variable is collating multiple data like this in the head above the tag manager code.
The process is, user lands on page, and clicks, this gets the data from wordpress etc using localized data and logs this information via ajax call. We wanted to also log this data in Google Analytics using the tag manager before the ajax call.
Here is the Javascript.
// JavaScript Document
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
};
/// Now ping event to google analytics - Nothing works
dataLayer.push(data);
dataLayer.push({'gg_event_action': event_action});
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
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);
});
My issue is not this code above so much its actually the fact that even after spending a day or so reading the documentation, watching videos I cannot seem to set up even one push that works.
In Tag manager:
- I created a new tag
- Chose Google Universal Analytics
- Chose custom event
- Set up a new macro
- Gave it a label name say gg_event_type
- Chose dataLayer Variable
But its not sending the data to Google Analytics. Any help as to what I am missing would be really appreciated.