Expand my Community achievements bar.

SOLVED

Getting text of selected item in drop down

Avatar

Former Community Member

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

Avatar

Correct answer by
Former Community Member

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

Avatar

Former Community Member

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

Avatar

Former Community Member

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

Avatar

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

Avatar

Former Community Member

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

Avatar

Correct answer by
Former Community Member

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

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----