Last active
March 7, 2016 20:52
-
-
Save 1forh/6900abe4cc6bafbb4f57 to your computer and use it in GitHub Desktop.
Two onclick events on one button -- Google Analytics Event Tracking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Track outbound links -- https://support.google.com/analytics/answer/1136920?hl=en */ | |
| /* Add second onclick event after original and return false for both */ | |
| <input type="submit" onclick="(function(x){return x;})(this); trackOutboundLink('http://website.com'); return false;"> | |
| <script> | |
| (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= | |
| function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date; | |
| e=o.createElement(i);r=o.getElementsByTagName(i)[0]; | |
| e.src='https://www.google-analytics.com/analytics.js'; | |
| r.parentNode.insertBefore(e,r)}(window,document,'script','ga')); | |
| ga('create','UA-XXXXXX-X','auto'); | |
| ga('send','pageview'); | |
| </script> | |
| /* Place this script below your GA code as I do here */ | |
| <script> | |
| /** | |
| * Function that tracks a click on an outbound link in Google Analytics. | |
| * This function takes a valid URL string as an argument, and uses that URL string | |
| * as the event label. Setting the transport method to 'beacon' lets the hit be sent | |
| * using 'navigator.sendBeacon' in browser that support it. | |
| */ | |
| var trackOutboundLink = function(url) { | |
| ga('send', 'event', 'outbound', 'click', url, { | |
| 'transport': 'beacon', | |
| 'hitCallback': function(){document.location = url;} | |
| }); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment