1

I need to set some field data for Google Analytics tracker manually in a certain situation. In my JS I have the following:

ga(function(tracker) {
   tracker.set('campaignKeyword', 'my keyword');
   tracker.send('event');
});

But I can't seem to see anything come through in Analytics.

I've run console.log(tracker) and it's a valid Universal Analytics tracking object — I'm just not sure what I'm doing wrong. I've been referencing this documentation.

What am I missing?

  • that documentation has the code ga('send', 'event', { 'eventCategory': 'Category', 'eventAction': 'Action' }); for sending an event. I don't see anywhere in there where tracker.send('event'); is supposed to do anything. I also shows the example of ga('set', 'campaignKeyword', 'Blue Shoes'); for setting the campaignKeyword. Where did you get the code you are using? – Stephen Ostermiller Sep 12 at 19:48
  • Looks like legacy ga.js code. However even if sending an event for legacy code.. still needs event category and event action at the very least.. they are required – Bronwyn V Sep 12 at 23:59
3

Try this code:

ga(function(tracker) {
  tracker.set('campaignKeyword', 'my keyword');
  tracker.set('eventCategory', 'my category');
  tracker.set('eventAction', 'my action');
  tracker.send('event');
});

Now you see the hit on Google Analytics, although this code is pretty ugly and not recommended by Google. :) Lets improve it:

ga('send', 'event', {
  eventCategory: 'my category', // required
  eventAction: 'my action',     // required
  campaignKeyword: 'my keyword' // optional
});

// or equivalent
ga('send', 'event', 'my category', 'my action', {
  campaignKeyword: 'my keyword'
});

The eventCategory and eventAction fields are required. If you don't pass them, you see the hit going on the browser's devtools network tab, but it will not be collected by GA servers.

Note: the previous commands only work if you have a tracker already created and named "t0", which is the default tracker. So, before those commands you must have something similar to ga("create", "<your-property-id", "auto").

You can use also the excelent Chrome extension named "Google Analytics Debugger", that print (to the console) debug information about your ga commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.