@gtm-support/vue-gtm
Version:
Simple implementation of Google Tag Manager for Vue
2 lines • 1.71 kB
JavaScript
import{GtmSupport as f,loadScript as m}from"@gtm-support/core";import{getCurrentInstance as y,nextTick as G}from"vue";import{assertIsGtmId as b,GtmSupport as I,hasScript as k,loadScript as L}from"@gtm-support/core";var i;function c(a,t={id:""}){t={trackOnNextTick:!1,...t},i=new f(t),a.config.globalProperties.$gtm=i,i.isInBrowserContext()&&(t.vueRouter&&v(a,t.vueRouter,t.ignoredViews,t.trackOnNextTick,t.vueRouterAdditionalEventData),i.options.enabled&&i.options.loadScript&&(Array.isArray(t.id)?t.id.forEach(r=>{if(typeof r=="string")m(r,t);else{let n={...t};r.queryParams!=null&&(n.queryParams={...n.queryParams,...r.queryParams}),m(r.id,n)}}):m(t.id,t))),a.provide("gtm",t)}function v(a,t,r=[],n,g=()=>({})){function p(e,u){return e instanceof Error?!!(e.type&u):!1}t.afterEach(async(e,u,l)=>{if(typeof e.name!="string"||Array.isArray(r)&&r.includes(e.name)||typeof r=="function"&&r(e,u))return;let s=e.meta&&typeof e.meta.gtm=="string"&&e.meta.gtm?e.meta.gtm:e.name;p(l,4)?i?.debugEnabled()&&console.log(`[VueGtm]: '${s}' not tracked due to navigation aborted`):p(l,8)&&i?.debugEnabled()&&console.log(`[VueGtm]: '${s}' not tracked due to navigation cancelled`);let d={...await g(e,u),...e.meta?.gtmAdditionalEventData},o=t.options?.history?.base??"";o.endsWith("/")||(o+="/"),o+=e.fullPath.startsWith("/")?e.fullPath.substring(1):e.fullPath,n?G(()=>{i?.trackView(s,o,d)}):i?.trackView(s,o,d)})}function V(a){return{install:t=>c(t,a)}}var O={install:c};var E=O;function R(){return y()?.appContext?.app?.config?.globalProperties?.$gtm??i}export{f as GtmPlugin,I as GtmSupport,b as assertIsGtmId,V as createGtm,E as default,k as hasScript,L as loadScript,R as useGtm};
//# sourceMappingURL=index.js.map