eskimo
eskimo

Reputation: 2624

ga send event in google analytics.js

I'm using Google analytics.js (so not the 'old' ga.js) and in some links I have for example:

<a href="www.example.com" onclick="ga('send', 'event', 'outbound link', 'position: header');">Link</a>

The onclick code is dynamically generated by php depending on what page you are. Now I can't test it yet since my account hasn't been converted into the new analytics.js, but I was wondering what I should put in the code in the <head> section of my page.

The docs states I should put

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y', 'auto');  // Creates a tracker.
ga('send', 'pageview');             // Sends a pageview.

But do I need to add ga('send', 'event') below that? Isn't really clear from the docs...

Upvotes: 1

Views: 16265

Answers (3)

Ishara Shehan
Ishara Shehan

Reputation: 446

You don't need to place the ga('send', 'event') inside the <head> code. You can use ga send event code in onclick event for any part of the webpage. Try this method to execute in anyplace you want to track such as a button,link,tabs,etc.

Upvotes: 4

Petr Havlik
Petr Havlik

Reputation: 3317

eskimo, the code that goes in the head is important as it creates the tracker object and so you can use all tracking functions once the page is loaded (therefore the create command, followed immediately by pageview request).

So no need to put anything that is related to sending events -- they should be used only with elements you want to track. I suggest using jQuery and adding event listener to make sure the event tracking is consistent.

Hope this helps :-)

Upvotes: 0

Ishara Shehan
Ishara Shehan

Reputation: 446

According to your statement (your question is not clear) , you are using universal analytics. Hence the new tracking code is different from the previous one and the code format you have pasted hear is also correct.

According to the GA help files we need to past in between <head> </head> tags. Once you done the needful you can execute the event tracking from any part form the webpage.

Upvotes: 0

Related Questions