UNPKG

automatic-analytics-trigger

Version:
2 lines (1 loc) 3.56 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).AutomaticAnalyticsTrigger=e()}(this,(function(){"use strict";var t={mutations:["show"],events:["click"],target:document.documentElement||document.body,maxAncestorsChecks:5};class e{constructor(t,e,s,i){this.target=e,this.events=t,this.dispatcheEvent=s,this.listeners={},this.finder=i}registerEventsListeners(){for(const t of this.events)this.listeners[t]=e=>this._eventHandler(e,"data-analytics-"+t),this.target.addEventListener(t,this.listeners[t],!1)}removeEventsListeners(){for(const t of this.events)this.target.removeEventListener(t,this.listeners[t],!1)}_eventHandler(t,e){const s=this.finder.firstWithAttribute(t,e);null!=s&&this.dispatcheEvent(s)}}class s{constructor(t=0){this.maxAncestorsChecks=t}firstWithAttribute(t,e){let s=t.composedPath();return s=this._removeWindowAndDocumentFromList(s),s=this._applyLimitIfNecessary(s),s.find(t=>t.hasAttribute(e))}_removeWindowAndDocumentFromList(t){return t.slice(0,t.length-2)}_applyLimitIfNecessary(t){return this._notNecessaryApplyLimit(this.maxAncestorsChecks)?t:t.slice(0,this.maxAncestorsChecks)}_notNecessaryApplyLimit(t){return 0==this.maxAncestorsChecks||t.length<=this.maxAncestorsChecks}}class i{constructor(t,e,s){this.target=e,this.mutations=t,this.dispatcheEvent=s}registerMutationsListeners(){this.observer=new MutationObserver(this._mutationHandler.bind(this)),this.observer.observe(this.target,{attributes:!0,childList:!0,subtree:!0,attributeFilter:["style"]})}removeMutationsListeners(){this.observer&&(this.observer.disconnect(),this.observer=null)}_mutationHandler(t){for(let e of t)this._checkShowAttributes(e),this._checkShowAddedNodes(e)}_checkShowAttributes(t){"attributes"===t.type&&n(t.target)&&"style"===t.attributeName&&a(t.target)&&this.dispatcheEvent(t.target)}_checkShowAddedNodes(t){if("childList"===t.type&&t.addedNodes.length>0)for(let e of t.addedNodes)n(e)&&a(e)&&this.dispatcheEvent(e)}}const n=t=>t.dataset&&t.hasAttribute(["data-analytics-show"]),a=t=>"none"!=t.style.display&&"hidden"!=t.style.visibility;return class{constructor(e,i){const n={...t,...i};this.callback=e,this.target=n.target,this.events=n.events,this.mutations=n.mutations,this.maxAncestorsChecks=n.maxAncestorsChecks,this.finder=new s(this.maxAncestorsChecks),this._dispatcheEventData=this._dispatcheEventData.bind(this)}init(){this.events&&(this.eventListener=new e(this.events,this.target,this._dispatcheEventData,this.finder),this.eventListener.registerEventsListeners()),this.mutations&&(this.mutationListener=new i(this.mutations,this.target,this._dispatcheEventData),this.mutationListener.registerMutationsListeners())}close(){this.events&&this.eventListener.removeEventsListeners(),this.mutations&&this.mutationListener.removeMutationsListeners()}_dispatcheEventData(t){const e=this._getEventDataFromDataAttributes(t.dataset);this.callback(e)}_getEventDataFromDataAttributes(t){const e=Object.keys(t).filter(t=>t.includes("analyticsDimension")).reduce((e,s)=>(e[s.replace("analyticsDimension","dimension")]=t[s],e),{});return{...t.analyticsEvent&&{event:t.analyticsEvent},...t.analyticsEventCategory&&{eventCategory:t.analyticsEventCategory},...t.analyticsEventAction&&{eventAction:t.analyticsEventAction},...t.analyticsEventLabel&&{eventLabel:t.analyticsEventLabel},...t.analyticsEventValue&&{eventValue:t.analyticsEventValue},...t.analyticsScreenName&&{screenName:t.analyticsScreenName},...Object.keys(e).length>0&&{eventDimensions:e}}}}}));