Select all class's with getElementsByClassName and click does not fire in dtm | Community
Skip to main content
New Participant
September 3, 2016
Solved

Select all class's with getElementsByClassName and click does not fire in dtm

  • September 3, 2016
  • 12 replies
  • 10994 views

Hi,

I have a carousel with 6 links on the homepage and am trying to capture each link click. I have set up a data element using css selector to capture the classname i.e. [class=navname]

I also created an event based rule with condition rule set to _satellite.setVar('Navigation' this.text) and added  an evar,prop and events . Issue is that this only works on the first click, and not the others. I need this to click on all the other links.

I tried changing the rule to document.getElementsByClassName{'navname').click(); return true and it still only captures the 1st click and when i clicked on the others i get the link name for the first item returned for all the links.

How do i get this to click on all of the classes named 'navname' and return the correct navigation title? I thought maybe i need to declare a variable or something in the code editor.

please I need help as am just getting familiar with DTM

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ParitMittal

Hi Kee, 

Please use the following code . We have tested it our end and it is working . 

var a= this.getElementsByTagName("span")[0].innerHTML).split("</span>");; var b= this.getElementsByTagName("span")[1].innerHTML; _satellite.setVar("Navigation",a[1].replace(/(\r\n|\n|\r)/gm,"") + "-" +b ); // This statement creates a Data Element  "Navigation" and stores the Name of the link in it console.log(a); console.log(b); return true;

Thanks & Regards

Parit Mittal

12 replies

KeeMacAuthor
New Participant
September 12, 2016

Thanks Parit

ParitMittal
ParitMittalAccepted solution
New Participant
September 8, 2016

Hi Kee, 

Please use the following code . We have tested it our end and it is working . 

var a= this.getElementsByTagName("span")[0].innerHTML).split("</span>");; var b= this.getElementsByTagName("span")[1].innerHTML; _satellite.setVar("Navigation",a[1].replace(/(\r\n|\n|\r)/gm,"") + "-" +b ); // This statement creates a Data Element  "Navigation" and stores the Name of the link in it console.log(a); console.log(b); return true;

Thanks & Regards

Parit Mittal

KeeMacAuthor
New Participant
September 7, 2016

am sure am doing something wrong

KeeMacAuthor
New Participant
September 7, 2016

when I entered the code below into my console i get this  Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '[object Window]' is not a valid selector.(…) error message

  1. var a= $(this)[0].getElementsByTagName("span")[1].getElementsByTagName("span")[0].innerHTML;
KeeMacAuthor
New Participant
September 7, 2016

Hi Parit,

Thanks very much for this. I just tried this and it doesn't seem to be working for me. When you say "To fetch the name of the navigation..." Does that mean I need to create a data element named "Navigation" and add the code as a custom script ?

Getting really confused now..:(

ParitMittal
New Participant
September 7, 2016

Hi Kee ,

As per our understanding , you want to track the the following Navigation link  marked Red on the carousel for the URL : https://uk.rimmellondon.com/ 

Hence , Please create an Event based rule with the following settings as per the CSS structure of the website :

Hence , To fetch the name of the Navigation Link clicked in a Data Element . Please use the following code:

var a= this.getElementsByTagName("span")[1].getElementsByTagName("span")[0].innerHTML; _satellite.setVar("Navigation",a ); return true;

Thanks & Regards

Parit Mittal

ParitMittal
New Participant
September 6, 2016

Hi, 

Thanks for providing the link. I am looking into the query and will get back to you ASAP.

Thanks & Regards

Parit Mittal

KeeMacAuthor
New Participant
September 6, 2016

i have dm'ed you the website

KeeMacAuthor
New Participant
September 5, 2016

Thanks

ParitMittal
New Participant
September 5, 2016

Hi , 

I need to understand the css structure of your website before any suggestions .Can you please share the URL of website as well your DTM account details in a private message

Thanks & Regards

Parit Mittal