javascript - GA send event, not working with custom dimensions -


i'm trying include list of custom dimensions when sending ga click event.

this code works:

ga('send', 'event', category, action, label, {   'hitcallback': function () {     //some code goes here   } }); 

but when adding custom dimensions:

ga('send', 'event', category, action, label, customdimensions.dimensionstosend, {    hitcallback': function () {       //some code    },    'hitcallbackfail': function () {       alert("unable send google analytics data");    } }); 

neither hitcallback nor hitcallbackfail gets fired. read in few places (like here) should able add custom dimensions ga send event function, maybe i'm doing wrong.

the problem second code block custom dimensions not being set appropriately. can't pass custom dimensions in further argument main ga() function. instead, need pass them in through fieldsobject (as part of object you're sending last argument), this:

ga('send', 'event', category, action, label, {    'dimension1': 'some value', // custom dimension 1    'dimension2': 'some value 2', // custom dimension 2    'hitcallback': function () {       //some code    } }); 

this shorthand for:

ga('send', {    'hittype': 'event',     'eventcategory': category,     'eventaction': action,     'eventlabel': label,    'dimension1': 'some value', // custom dimension 1    'dimension2': 'some value 2', // custom dimension 2    'hitcallback': function () {       //some code    } }); 

see ga() command queue reference.

note hitcallbackfail not valid field; won't called on failure.

see list of available fields.

in fact, function provide hitcallback executed whether hit sent successful or if google analytics rejected hit. times won't executed if google analytics library not load, or if server connection fails.

if have code want execute if server fails, can use timeout. see example above page:

// use timeout ensure execution of critical application code. ga('send', 'pageview', {'hitcallback': criticalcode}); settimeout(criticalcode, 2000);  // run critical code once. var alreadycalled = false; function criticalcode() {   if (alreadycalled) return;   alreadycalled = true;    // run critical code here... } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -