With the old version of Analytics I was tracking events programmatically, like this:
_gaq.push(['_trackEvent', 'my-category', 'my-action', 'my-label'])
With new Universal Analytics, the tracking is done using:
ga('send', 'event', 'my-category', 'my-action', 'my-label');
Both of these create new events in Google Analytics panel, grouped by category and action.
How can I do it with Google Tag Manager? I have added Universal Analytics as a tag in the GTM panel. Now I need to add a piece of code to trigger the events, something like this?
dataLayer.push({'event': ???, 'category':xx, 'action';:xx, 'label':xx});
I do not want to add anything in the GTM admin panel. I would just like to add a piece of code on my website to track the events programmatically and the events to show up in the Google Analytics panel.
You have to add something in the admin panel, else this will not work.
"Event" in the dataLayer means something different than event tracking in GA - Google choose to confuse the heck out of everybody by using the same name for pretty much unrelated concepts (that those share the name with javascript events does not make things any easier).
"Event" in GTM/in the dataLayer is a reserved variable name that is often (but not always) automatically populated by GTM itself. An event is what triggers a tag to fire. This might be a pageview, a click or a custom event in the dataLayer. So a dataLayer event might be used to trigger an Google Analytics event, but it does send not any data by itself (as GTM and GA are completely independent from each other).
So you need to create a separate GA tag in your GTM interface and change the interaction type from "pageview" to "event". The fields of the tag template will change to reflect this setting, i.e. you will get fields for category etc.
You would then create three macros of the "dataLayer" type and set the "Data Layer Variable Name" to category, action and label respectively to pick up the values that you have set up in your dataLayer. Then enter the macros in the respective fields in the GA event trackins tag.
Finally you need a (GTM) event to create a rule to fire the tag. On Pageview/DOM Ready/Page loaded the event is automatically populated (gtm.js, gtm.dom or gtm.load), for clicks and submits there are autoevent handlers [1] (with their events gtm.click, gtm.linkClick, gtm.formSubmit) or you can use a custom event, i.e. set the "event" variable in the dataLayer to a custom value, let's say "ga_event".
Then you can create a new rule where event eq ga_event and use it to fire the new Analytics event tracking tag. Create version, publish, and there you are.
But you can not send any tracking calls from the dataLayer directly, that would defeat the purpose of the tag manager.
[1] So far you needed to set up autoevent handlers as separate tags. The new GTM interface will take care of this automatically.