-
Notifications
You must be signed in to change notification settings - Fork 0
/
GAHelper.min.js
4 lines (4 loc) · 3.39 KB
/
GAHelper.min.js
1
2
3
4
/**
* @author Aaron Clinger - https://github.com/aaronclinger/gahelper.js
*/
!function(t,e){"use strict";function n(){var n={},a=!0,r="navigator"in t&&"sendBeacon"in t.navigator,i=!1;n.forceTry=!1,n.timeout=2e3,n.trackerName=null,n.create=function(t){return n.isDefined()||c(),"string"==typeof t&&(t={trackingId:t}),t.name?n.trackerName=t.name=u(t.name):"string"==typeof n.trackerName&&(t.name=u(n.trackerName)),t.cookieDomain=t.cookieDomain||"auto",o()("create",t),n},n.pageview=function(e){var r,i;return(e=e||{}).hitType="pageview",i=l("set",e),e.page||e.location||(o()(i,"location",t.location.toString()),o()(i,"page",t.location.pathname.toString())),e.clearUTM&&(a&&(a=!1,r=e.hitCallback,e.hitCallback=function(t){n.clearUTM(),r&&r(t)}),delete e.clearUTM),n.send(e)},n.event=function(t){return t.hitType="event",n.send(t)},n.send=function(t){var e=!1,a,i,c;return t.hitCallback&&(a=t.hitCallback,i=function(t){e||(e=!0,clearTimeout(c),a(t))},t.hitCallback=function(){i(!0)},c=setTimeout((function(){i(!1)}),n.timeout)),!t.transport&&r&&(t.transport="beacon"),n.forceTry||n.isLoaded()||n.isDefined()&&"pageview"===t.hitType?o()(l("send",t),t):t.hitCallback&&t.hitCallback(!1),n},n.initAttributeTracking=function(){return i||(i=!0,"interactive"===e.readyState||"complete"===e.readyState?m():e.addEventListener("DOMContentLoaded",m)),n},n.clearUTM=function(){var e=t.location.toString(),a;return"history"in t&&"pushState"in t.history&&-1!==e.indexOf("?")&&(e=(e=e.replace(/utm_(?:source|medium|term|content|campaign)=[^\&#]+\&*/gi,"")).replace(/(\?|\&)+(?=$|#)/g,""),t.history.replaceState({},"",e)),n},n.isDefined=function(){return!!o()},n.isLoaded=function(){return n.isDefined()&&o().hasOwnProperty("loaded")&&!0===o().loaded};var c=function(){var n,a,r,i,c,o,u;a=e,r="script",i="//www.google-analytics.com/analytics.js",c="ga",(n=t).GoogleAnalyticsObject=c,n.ga=n.ga||function(){(n.ga.q=n.ga.q||[]).push(arguments)},n.ga.l=1*new Date,o=a.createElement(r),u=a.getElementsByTagName(r)[0],o.async=1,o.src=i,u.parentNode.insertBefore(o,u)},o=function(){return t.ga},u=function(t){return t.replace(/^[ \t\n\r]+|[ \t\n\r]+$/g,"")},l=function(t,e){var a=e.name||n.trackerName;return"string"==typeof a&&""!==(a=u(a))?a+"."+t:t},s=function(t){var e=t.getAttribute("data-track")||t.getAttribute("data-track-async");if(null===e||""===e)return{};for(var n={},a=e.split(","),r=["eventCategory","eventAction","eventLabel","eventValue"],i=Math.min(4,a.length);i--;)n[r[i]]=u(a[i]);return n},f=function(t){return"form"===t.tagName.toLowerCase()},v=function(t){var a=t.target;if(!f(a)){var i=a.href,c=a.target,o=c&&"_blank"===c.toLowerCase(),u=s(a);!i||o||r||(t.preventDefault(),u.hitCallback=function(){e.location=i}),n.event(u)}a.removeEventListener("click",v)},g=function(t){var e=t.target;n.event(s(e)),e.removeEventListener("click",g)},d=function(t){var a=t.target;if(a.getAttribute("data-track-async"))n.event(s(a));else if(a.getAttribute("data-track")){var i=s(a);r||(t.preventDefault(),i.hitCallback=function(){e.removeEventListener("submit",d),a.submit()}),n.event(i)}},m=function(){e.addEventListener("mousedown",(function(t){var e=t.target;if(e.getAttribute("data-track")){var n=1===t.which&&(t.shiftKey||t.altKey||t.metaKey||t.ctrlKey);e.addEventListener("click",n?g:v)}})),e.addEventListener("click",(function(t){var e=t.target;e.getAttribute("data-track-async")&&!f(e)&&n.event(s(e))})),e.addEventListener("submit",d)};return n}t.GAHelper=new n}(window,document);