Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Getting text of selected item in drop down

wgra021
Level 1
Level 1

We have a rule that fires on a change event for a drop down list & are having trouble figuring out how we can get the text of the selected item.

Using %this.@text% in the GA part of the rule doesn't work. As in this case, "this" will be the select element for the drop down not the selected option.

Any help how we can do this would be much appreciated.

Cheers Wayne

1 Accepted Solution
wgra021
Correct answer by
Level 1
Level 1

I tried data elements again, which sort of lead me in the correct direction. Ended up finding that I couldn't use predefined data elements for this, due to the timing of when the data element is set & when change event fires.

I ended up having to add a custom condition to the rule that populated a data element (not predefined). Below is some sample code for anyone trying to achieve the same thing.

var selectEl = document.getElementById("dropdown-to-get-selected-option-text-from"); _satellite.setVar("selected-option-text", selectEl.options[selectEl.selectedIndex].text); return true;

I was then able to use the variable in the action part of the DTM rule. Hope this helps anyone trying to do the same thing in the future.

Cheers Wayne

View solution in original post

5 Replies
AllDayDev
Employee
Employee

Hi Wayne,

I'm not sure on the proper syntax for GA, but in JavaScript you'd do it something like:

this.options[this.selectedIndex].text

 

- Carey

wgra021
Level 1
Level 1

Thanks for the reply Carey.

I should've been a little more clear about where I'm trying to get at the text for the selected item, it's in the pageview part of the DTM rule in the universal analytics section.

I've tried using plain old javascript in there to get at the text of the selected item like you suggested, but it doesn't work. I was hoping we might be able to get at it using the variables available in DTM. A quick overview is included in this cheatsheet -

http://jimalytics.com/wp-content/uploads/2014/10/DTM-Cheat-Sheet.pdf

Cheers Wayne

Rahsing
Level 8
Level 8

Hi Wayne,

Are you using data elements for capturing the text? 

If not, I suppose you could use it and then reference the data element in the %element.name% format (as is mentioned in the cheat sheet you have).

Let me know if that works.

Also, in case you wish to understand more about data elements, here's some documentation.

Rahul

wgra021
Level 1
Level 1

Thanks Rahul. I had tried using data elements previously, but not sure if I got it quite right.

I'll give it another go & post what I find back here, regardless of if I'm successful or not.

Cheers Wayne

wgra021
Correct answer by
Level 1
Level 1

I tried data elements again, which sort of lead me in the correct direction. Ended up finding that I couldn't use predefined data elements for this, due to the timing of when the data element is set & when change event fires.

I ended up having to add a custom condition to the rule that populated a data element (not predefined). Below is some sample code for anyone trying to achieve the same thing.

var selectEl = document.getElementById("dropdown-to-get-selected-option-text-from"); _satellite.setVar("selected-option-text", selectEl.options[selectEl.selectedIndex].text); return true;

I was then able to use the variable in the action part of the DTM rule. Hope this helps anyone trying to do the same thing in the future.

Cheers Wayne

View solution in original post