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
2K members online now
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

adding Event tracking code to my page

Follower ✭ ✭ ☆
# 1
Follower ✭ ✭ ☆

Hi and Help!

I set up my goals, four of them for a WordPress page I'm testing.

I was revisiting the Event tracking module in the academy only to realize it DOES NOT give details or examples of how to put the code in my page. What I mean by that is, I wanted to track clicks on 4 elements on the page. I set up the event and and put href links on 3 images and a text. So one of my events would be: 

"ga('send', 'event', 'Img1', 'Click', 'First.Image.Click');"

BUT I have no idea IF I'm to add this to the a href=... tag or ??? This is the one little glitch I realized I had no idea how to do. I checked the developer page and got the format of the code but no idea how to add it to the element I want to track.

how do I integrate this ga event tracking code in my WP page for this specific element??

Thanks

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Dianne D
June 2017

Re: adding Event tracking code to my page

Top Contributor
# 2
Top Contributor

Hi :-)

you could add it to the ahref tag using an onclick like the following

<a href="http://www.yourlinktosomewhere.com" onclick="ga('send', 'event', 'Img1', 'Click', 'First.Image.Click');"><img src="pathto/yourimage.jpg" /></a>

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author Dianne D
June 2017

Re: adding Event tracking code to my page

Top Contributor
# 2
Top Contributor

Hi :-)

you could add it to the ahref tag using an onclick like the following

<a href="http://www.yourlinktosomewhere.com" onclick="ga('send', 'event', 'Img1', 'Click', 'First.Image.Click');"><img src="pathto/yourimage.jpg" /></a>

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

adding Event tracking code to my page

Participant ✭ ✭ ☆
# 3
Participant ✭ ✭ ☆

Hi @Dianne D,

 

As @Bronwyn V mentioned, you can add your event command with event attribute to your element. Alternatively, you can use event listeners to achieve this.

Example: 

<script>

document.getElementById("yourElementId").addEventListener("click", function(){ ga('send', 'event', 'Img1', 'Click', 'First.Image.Click')});

</script>

 

or

 

<script>

function sendGAEvent(){

   ga('send', 'event', 'Img1', 'Click', 'First.Image.Click');

}

document.getElementById("yourElementId").addEventListener("click", sendGAEvent());

</script>

 

I hope this helps,

Regards

adding Event tracking code to my page

Follower ✭ ✭ ☆
# 4
Follower ✭ ✭ ☆

@Gny Thanks so much for the alternative. I need to rejigger my JavaScript knowledge. It's been a while and now that​ I need it it's more of a priority. I am keeping this idea for future reference. Cheers!

Re: adding Event tracking code to my page

Follower ✭ ✭ ☆
# 5
Follower ✭ ✭ ☆

@Bronwyn V THANK YOU!! The onclick is the piece of the puzzle I was missing!!