Search code examples
google-analyticsuniversal-analytics

Google Universal Analytics Event Tracking for Select Box


I am using the new Google Universal Analytics code.

Is there a way to add event tracking to a select box and track clicks on every single option within the select box?

(I did find some similar questions but all without answer. So I am thankful for answers from everyone knowing more than us.)

Universal Analytics Code:

<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-XXXXXXX-1', 'auto');
ga('send', 'pageview');
</script>

Select element code:

<select>
    <option selected="selected">All</option>
    <option value="1">USA</option>
    <option value="2">Canada</option>
    <option value="3">UK</option>
    <option value="4">Australia</option>
</select>

Solution

  • Something like this should work

    <select onChange="ga('send', 'event', 'Form Event', 'Select Changed', this.options[this.selectedIndex].text);">
        <option selected="selected">All</option>
        <option value="1">USA</option>
        <option value="2">Canada</option>
        <option value="3">UK</option>
        <option value="4">Australia</option>
    </select>
    

    If you want to save the value instead use this.options[this.selectedIndex].value instead.