@elbwalker/destination-web-google-ga4
Version:
Google GA4 web destination for walkerOS
1 lines • 7.02 kB
Source Map (JSON)
{"version":3,"sources":["../src/parameters.ts","../src/types/index.ts","../src/index.ts"],"sourcesContent":["import type { WalkerOS } from '@elbwalker/types';\nimport type { Include, Parameters } from './types';\n\nexport function getParamsInclude(\n event: WalkerOS.DeepPartialEvent,\n include: Include,\n): Parameters {\n const params: Parameters = {};\n\n // Check for the 'all' group to add each group\n if (include.includes('all'))\n include = [\n 'context',\n 'data',\n 'event',\n 'globals',\n 'source',\n 'user',\n 'version',\n ];\n\n include.forEach((groupName) => {\n let group = event[groupName as keyof Omit<WalkerOS.Event, 'all'>] || {};\n\n // Create a fake group for event properties\n if (groupName == 'event')\n group = {\n id: event.id,\n timing: event.timing,\n trigger: event.trigger,\n entity: event.entity,\n action: event.action,\n group: event.group,\n count: event.count,\n };\n\n Object.entries(group).forEach(([key, val]) => {\n // Different value access for context\n if (groupName == 'context') val = (val as WalkerOS.OrderedProperties)[0];\n\n params[`${groupName}_${key}`] = val;\n });\n });\n\n return params;\n}\n","import type { Mapping } from '@elbwalker/types';\nimport type { DestinationWeb } from '@elbwalker/walker.js';\n\ndeclare global {\n interface Window {\n dataLayer: Array<unknown> | unknown;\n gtag: Gtag.Gtag;\n }\n}\n\nexport interface Destination\n extends DestinationWeb.Destination<Custom, CustomEvent> {}\n\nexport type Config = DestinationWeb.Config<Custom, CustomEvent>;\n\nexport interface Custom {\n measurementId: string;\n debug?: boolean;\n include?: Include;\n pageview?: boolean;\n server_container_url?: string;\n snakeCase?: boolean;\n transport_url?: string;\n}\n\nexport type EventConfig = Mapping.EventConfig<CustomEvent>;\n\nexport interface CustomEvent {\n include?: Include;\n}\n\nexport type Param = Mapping.Value;\nexport type Include = Array<\n | 'all'\n | 'context'\n | 'data'\n | 'event'\n | 'globals'\n | 'source'\n | 'user'\n | 'version'\n>;\n\nexport type Parameters = Gtag.ControlParams &\n Gtag.EventParams &\n Gtag.CustomParams;\n","import type { WalkerOS } from '@elbwalker/types';\nimport type { Custom, Destination, Parameters } from './types';\nimport { getParamsInclude } from './parameters';\nimport { isObject } from '@elbwalker/utils';\n\n// Types\nexport * as DestinationGoogleGA4 from './types';\n\nexport const destinationGoogleGA4: Destination = {\n type: 'google-ga4',\n\n config: { custom: { measurementId: '' } },\n\n init(config = {}) {\n const w = window;\n const { custom = {} as Partial<Custom>, fn, loadScript } = config;\n const { measurementId, transport_url, server_container_url, pageview } =\n custom;\n\n if (!measurementId) return false;\n\n // Load the gtag script\n if (loadScript) addScript(measurementId);\n\n const settings: WalkerOS.AnyObject = {};\n\n // custom transport_url\n if (transport_url) settings.transport_url = transport_url;\n\n // custom server_container_url\n if (server_container_url)\n settings.server_container_url = server_container_url;\n\n // disable pageviews\n if (pageview === false) settings.send_page_view = false;\n\n // setup required methods\n w.dataLayer = w.dataLayer || [];\n\n let func = fn || w.gtag;\n if (!w.gtag) {\n w.gtag = function () {\n (w.dataLayer as unknown[]).push(arguments);\n };\n func = func || w.gtag;\n func('js', new Date());\n }\n\n // gtag init call\n func('config', measurementId, settings);\n },\n\n push(event, config, mapping = {}, options = {}) {\n const { custom, fn } = config;\n const customEvent = mapping.custom || {};\n if (!custom) return;\n\n if (!custom.measurementId) return;\n\n const data = isObject(options.data) ? options.data : {};\n\n const paramsInclude = getParamsInclude(\n event,\n // Add data to include by default\n customEvent.include || custom.include || ['data'],\n );\n\n const eventParams: Parameters = {\n ...paramsInclude,\n ...data,\n };\n\n // Event name (snake_case default)\n let eventName = event.event; // Assume custom mapped name\n if (!mapping.name && custom.snakeCase !== false)\n // Use snake case if not mapped or disabled\n eventName = eventName.replace(' ', '_').toLowerCase();\n\n // Set the GA4 stream id\n eventParams.send_to = custom.measurementId;\n\n // Debug mode\n if (custom.debug) eventParams.debug_mode = true;\n\n const func = fn || window.gtag;\n func('event', eventName, eventParams);\n },\n};\n\nfunction addScript(\n measurementId: string,\n src = 'https://www.googletagmanager.com/gtag/js?id=',\n) {\n const script = document.createElement('script');\n script.src = src + measurementId;\n document.head.appendChild(script);\n}\n\nexport default destinationGoogleGA4;\n"],"mappings":";AAGO,SAAS,iBACd,OACA,SACY;AACZ,QAAM,SAAqB,CAAC;AAG5B,MAAI,QAAQ,SAAS,KAAK;AACxB,cAAU;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAEF,UAAQ,QAAQ,CAAC,cAAc;AAC7B,QAAI,QAAQ,MAAM,SAA8C,KAAK,CAAC;AAGtE,QAAI,aAAa;AACf,cAAQ;AAAA,QACN,IAAI,MAAM;AAAA,QACV,QAAQ,MAAM;AAAA,QACd,SAAS,MAAM;AAAA,QACf,QAAQ,MAAM;AAAA,QACd,QAAQ,MAAM;AAAA,QACd,OAAO,MAAM;AAAA,QACb,OAAO,MAAM;AAAA,MACf;AAEF,WAAO,QAAQ,KAAK,EAAE,QAAQ,CAAC,CAAC,KAAK,GAAG,MAAM;AAE5C,UAAI,aAAa,UAAW,OAAO,IAAmC,CAAC;AAEvE,aAAO,GAAG,SAAS,IAAI,GAAG,EAAE,IAAI;AAAA,IAClC,CAAC;AAAA,EACH,CAAC;AAED,SAAO;AACT;A;;;;;;;;;;AC7CA;;;ACQO,IAAM,uBAAoC;AAAA,EAC/C,MAAM;AAAA,EAEN,QAAQ,EAAE,QAAQ,EAAE,eAAe,GAAG,EAAE;AAAA,EAExC,KAAK,SAAS,CAAC,GAAG;AAChB,UAAM,IAAI;AACV,UAAM,EAAE,SAAS,CAAC,GAAsB,IAAI,WAAW,IAAI;AAC3D,UAAM,EAAE,eAAe,eAAe,sBAAsB,SAAS,IACnE;AAEF,QAAI,CAAC,cAAe,QAAO;AAG3B,QAAI,WAAY,WAAU,aAAa;AAEvC,UAAM,WAA+B,CAAC;AAGtC,QAAI,cAAe,UAAS,gBAAgB;AAG5C,QAAI;AACF,eAAS,uBAAuB;AAGlC,QAAI,aAAa,MAAO,UAAS,iBAAiB;AAGlD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