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

Please Help:Tracking Link hard coded in GTM setup

Explorer ✭ ✭ ✭
# 1
Explorer ✭ ✭ ✭

Hello All,

Suppose I have a website with GTM and Google analytics installed via GTM.

Now if i try to track any thing suppose an event hard coded without the help of  gtm does it work?

Suppose I have put the below code:

<a href="mailto:sales@xyz.com"onclick="ga('send','event','email',' emaillinkclicked','sales@xyz.com');">sales@xyz.com</a>

 

does it send an event to GA?or i have to do it only via GTM?

Please Help:Tracking Link hard coded in GTM setup

[ Edited ]
Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

Hi Soumik,

 

It would mostly likely send 9/10 times, but wouldn't be 100% reliable since you aren't sure at the time of loading the page that ga() function is available ,since it is being loaded asynchronously

 

I would recommend attaching an event to the element via GTM, there isn't much reason to hardcode onclick events.

 

In your case:

someElement.addEventListener('click', function() {

  ga('send','event','email',' emaillinkclicked','sales@xyz.com');

});

 

You could also make the event label dynamic by reading it's value from the anchor tag text.

 

Regards

Andy

Please Help:Tracking Link hard coded in GTM setup

[ Edited ]
Explorer ✭ ✭ ✭
# 3
Explorer ✭ ✭ ✭

Thanks for the reply.

One thing  i do not understand how to modify put the code here:

 

My code:

<a href="mailto:sales@xyz.com"onclick="ga('send','event','email',' emaillinkclicked','sales@xyz.com');">sales@xyz.com</a>

 

 

Your code:

someElement.addEventListener('click', function() {

  ga('send','event','email',' emaillinkclicked','sales@xyz.com');

});

 

What should i change in my code?

Can u please put the full code?

 

I want to track mail to links in this.I know it is easier in GTM but I just want to know what is the modified code in this case

 

<a href="mailto:sales@xyz.com"onclick="ga('send','event','email',' emaillinkclicked','sales@xyz.com');">sales@xyz.com</a>

 

Should it is like this below?

mailto:sales@xyz.com is our element in this case

 

Suppose after enabling builtin click variables like click text,click element,in GTM 'click text' automatically populates our value mailto:sales@xyz.com

then is the below code work

click text.addEventListener('click', function() {

  ga('send','event','email',' emaillinkclicked','sales@xyz.com');

});

 

I know  i am making this complicated.Just for knowledge.I am asking.

 

In GTM it is lot of easier to track this things.I know it.So i think if i use gtm use it in gtm way,if we use hard coded analytics without gtm my first hard coded code will work.