Update – April 16th, 2014: a new article on how to achieve this more efficiently specifically with GA Universal Code was posted. The below idea is still valid in some cases.
Is it possible to use Event tracking with links/forms that redirect to pages in the same browser window?
I used to think that this was not possible, but after realizing Adobe SiteCatalyst/Omniture has a mechanism* to deal with this, I realized that I could replicate similar behaviour in GA (Universal or Classic). To get it to work relies on JavaScript and not the analytics package itself.
That being said, you can generalize this technique with any package that you’re using, but I personally use GA more, so my examples are for this package.
I want to caveat this solution that you should have a decent understand of JS and what we’re doing as it can have repercussions on other aspects of your site/tracking if the techniques are not understood or implemented properly.
Nevertheless, the general idea is:
- User executes an action (e.g. clicks a link or submits a form)
- JS prevents the default behavior of that click (prevents default event and stops event propagation)
- Send an Event request to the package/GA and delay for 500ms – this portion is key so that we can ensure that the request it tracked/sent before pushing a user off the page*
- After the timeout, the JS send the user along to what they originally intended
What do I need to do?
I’ve provided a vanilla JS example (the concepts are easily translatable to jQuery or any other JS framework – but for simplicity sake I’m demoing the concept) where I attach an event call with the “onsubmit” or “onclick” handlers in the HTML. The code sample below should be commented and explain what is going on:
- <!-- When a user submits the form - delay for 500 MS so the event can be sent in -->
- <form name="form1" method="post" action="" onsubmit="return sendGA(this, ['mysubmissioncategory', 'mysubmissionaction', 'mysubmission'])">
- <input type="text" name="test" id="test">
- <input type="submit" name="button" id="button" value="Submit">
- </form>
- <hr />
- <!-- When a user click the link - delay for 500 MS so the event can be sent in -->
- <script>
- //first parameter - the element that was clicked/submitted
- //array of strings where the first index is the category, second index is the action and the third index is the label
- function sendGA(el, gaevent){
- var newwin = false;
- //assume/ensure anything that is tracked has a href or an action attribute
- if(el.hasAttribute("href") || el.hasAttribute("action")){
- //simplifying my example to just account for new _blank windows - you may need to modify this according to your needs
- if(el.hasAttribute("target") && el.getAttribute("target") == "_blank"){
- newwin = true;
- }else{
- //delay the default action for 500 ms - for whatever tag so that the Analytics request can be sent
- setTimeout(function() {
-
- //is the originating element a form, if so make it submit
- if(el.tagName == "FORM"){
- el.submit();
- }else{
- //assume that if it is not a form that it a link
- window.location = el.getAttribute("href");
- }
-
- }, 500);
- }
- //send the event!
- ga('send', 'event', gaevent[0], gaevent[1], gaevent[2]);
- // If you are using the Classic analytics event tracking, just comment the above line out and uncomment one of the lines below
- // depending on the GA code snippet that your site is usinmg
- //_gaq.push(['_trackEvent', gaevent[0], gaevent[1], gaevent[2]]);
- //pageTracker._trackEvent(gaevent[0], gaevent[1], gaevent[2]])
-
- return newwin;
- }
- }
- </script>