AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
4.2K members online now
4.2K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Setting up Event Tracking

Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Howdy!

I'm confuse on setting up my even tracking code. Should I separate it in other java script file or just put it on my google analytics tracking code? See my planned implementation (look at the bold text):

 

<script>
(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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-00000000-1', 'auto');
ga('send', 'pageview');

 

ga('send', {
hitType: 'event',
eventCategory: 'Videos',
eventAction: 'play',
eventLabel: 'Nonsense Campaign'
});


</script>

 

Setting up Event Tracking

A T
Participant ✭ ✭ ✭
# 2
A T
Participant ✭ ✭ ✭

Event tracking code needs to be added to an event handler so that it only triggers when the user interaction occurs not with the default page view.

 

do see: https://support.google.com/analytics/topic/7330657

Setting up Event Tracking

Follower ✭ ☆ ☆
# 3
Follower ✭ ☆ ☆

Can you please elaborate it? 

Setting up Event Tracking

Participant ✭ ✭ ☆
# 4
Participant ✭ ✭ ☆

Hi Jarric,
as @A T mentioned, event should be sen with an event handler. In other words, It should be triggered on the user action that you want to track with your event.

Based on your example, Let's say you have a button with an Id "myButton" then you should send the event when user clicks on the play button.
Implementation is as following (Please do not forget, when you use even listeners, put your event code before closing 'body' tag):

<script>
document.getElementById("myButton").addEventListener("click", function(){
   ga('send', 'event', 'Videos', 'Play', 'Nonsense Campaign');
});
</script>

 

I hope this solves your problem.
Regards

Setting up Event Tracking

Follower ✭ ☆ ☆
# 5
Follower ✭ ☆ ☆

Still a little bit confuse but I got your point.. Ill try this and let us see. Thank you so much!

Setting up Event Tracking

Participant ✭ ✭ ☆
# 6
Participant ✭ ✭ ☆

Hi Jarric,

 

When you use Google Analytics event command (ga('send', 'event', 'Videos', 'Play', 'Nonsense Campaign')Smiley Wink in your GA tracker snippet, it will send this event whenever a user views the page because it will be triggered on the page load. Since you want to track the users who click on the play button, you should send your event when the user clicks on the play button.

 

In order to do this, you should use an event listener(my previous example) or you can set it on "onclick" as inline. When you do this, the event will be triggered only when user clicks on the play button.

 

If my previous example is complicated for you, you can also do following: put your event code inside "onclick" attribute of your play button.

<button onclick="ga('send', 'event', 'Videos', 'Play', 'Nonsense Campaign');">Play</button>

 

I hope this clarifies and helps you to solve your problem

You're welcome!