UNPKG

@elbwalker/destination-web-google-ga4

Version:

Google GA4 web destination for walkerOS

1 lines 1.55 kB
function t(t){return"object"==typeof t&&null!==t&&!function(t){return Array.isArray(t)}(t)&&"[object Object]"===Object.prototype.toString.call(t)}var e={},n={type:"google-ga4",config:{custom:{measurementId:""}},init(t={}){const e=window,{custom:n={},fn:a,loadScript:r}=t,{measurementId:o,transport_url:c,server_container_url:i,pageview:s}=n;if(!o)return!1;r&&function(t,e="https://www.googletagmanager.com/gtag/js?id="){const n=document.createElement("script");n.src=e+t,document.head.appendChild(n)}(o);const u={};c&&(u.transport_url=c),i&&(u.server_container_url=i),!1===s&&(u.send_page_view=!1),e.dataLayer=e.dataLayer||[];let g=a||e.gtag;e.gtag||(e.gtag=function(){e.dataLayer.push(arguments)},g=g||e.gtag,g("js",new Date)),g("config",o,u)},push(e,n,a={},r={}){const{custom:o,fn:c}=n,i=a.custom||{};if(!o)return;if(!o.measurementId)return;const s=t(r.data)?r.data:{},u=function(t,e){const n={};return e.includes("all")&&(e=["context","data","event","globals","source","user","version"]),e.forEach((e=>{let a=t[e]||{};"event"==e&&(a={id:t.id,timing:t.timing,trigger:t.trigger,entity:t.entity,action:t.action,group:t.group,count:t.count}),Object.entries(a).forEach((([t,a])=>{"context"==e&&(a=a[0]),n[`${e}_${t}`]=a}))})),n}(e,i.include||o.include||["data"]),g={...u,...s};let d=e.event;a.name||!1===o.snakeCase||(d=d.replace(" ","_").toLowerCase()),g.send_to=o.measurementId,o.debug&&(g.debug_mode=!0);(c||window.gtag)("event",d,g)}};var a=n;export{e as DestinationGoogleGA4,a as default,n as destinationGoogleGA4};//# sourceMappingURL=index.mjs.map