Last active
September 5, 2016 10:32
-
-
Save rmpel/e39b712ec0d1eff0eb068273af042a9b to your computer and use it in GitHub Desktop.
Always-working Google Analytics Event/Pageview code
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
| (function () { | |
| /** | |
| * Track a virtual pageview | |
| * @param url String the URL to send to GA. Developer will need to prefix as needed. /virtual/ for virtual pages, /download/ for downloads are a good startingpoint. | |
| * @param title String (optional) Title to send with the page-hit. document.title will be used if omitted. | |
| * @returns {Number} the result of the ga(), _gaq.push() or pageTracker._trackPageview call | |
| */ | |
| window.gaPageview = function (url, title) { | |
| title = title || false; | |
| if (!title) { | |
| title = document.title; | |
| } | |
| if ( 'undefined' !== typeof dataLayer ) { | |
| return dataLayer.push({ | |
| 'event':'VirtualPageview', | |
| 'virtualPageURL': url, | |
| 'virtualPageTitle' : title, | |
| }); | |
| } | |
| if ('function' === typeof ga) { | |
| return ga('send', {'hitType': 'pageview', 'page': url, 'title': title}); | |
| } | |
| if ('undefined' !== typeof _gaq) { | |
| _gaq.push(["_set", "title", title]); | |
| return _gaq.push(['_trackPageview', url]); | |
| } | |
| // the SYNC tracker usually exposes the _gaq object, but in case it does not... | |
| if ('undefined' !== typeof pageTracker && 'function' === typeof pageTracker._trackPageview) { | |
| // temporarily set document.title to the wished-for title; there is no other way to get the correct title in analytics with this tracking system | |
| var oldTitle = document.title; | |
| document.title = title; | |
| var result = pageTracker._trackPageview(url); | |
| document.title = oldTitle; | |
| return result; | |
| } | |
| console.error("No valid tracker agent could be found. Did NOT send pageview."); | |
| }; | |
| /** | |
| * Track an event | |
| * @param category String the event category | |
| * @param action String the event action | |
| * @param opt_label String (optional) event Label | |
| * @param opt_value String (optional) event Value | |
| * @param opt_noninteraction Boolean (optional) nonInteraction yes or no (default is no for interactive) | |
| * @returns {Number} the result of the ga() or _gaq.push() call | |
| */ | |
| window.gaEvent = function (category, action, opt_label, opt_value, opt_noninteraction) { | |
| if ( 'undefined' !== typeof dataLayer ) { | |
| return dataLayer.push({ | |
| 'event':'virtualEvent', | |
| 'eventCategory': category, | |
| 'eventAction' : action, | |
| 'eventLabel' : opt_label, | |
| 'eventValue' : opt_value, | |
| 'eventNonInteraction': opt_noninteraction | |
| }); | |
| } | |
| if ('function' === typeof ga) { | |
| return ga('send', 'event', category, action, opt_label, opt_value, { nonInteraction: opt_noninteraction }); | |
| } | |
| if ('undefined' !== typeof _gaq) { | |
| var parameters = arguments; | |
| parameters.unshift('_trackEvent'); | |
| return _gaq.push(parameters) | |
| } | |
| // the SYNC tracker usually exposes the _gaq object, but in case it does not... | |
| if ('undefined' !== typeof pageTracker && 'function' === typeof pageTracker._trackEvent) { | |
| return pageTracker._trackEvent(category, action, opt_label, opt_value, opt_noninteraction ); | |
| } | |
| console.error("No valid tracker agent could be found. Did NOT send event."); | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment