@elbwalker/destination-web-google-ads
Version:
Google Ads web destination for walkerOS
1 lines • 3.42 kB
Source Map (JSON)
{"version":3,"sources":["../src/types/index.ts","../src/index.ts"],"sourcesContent":["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 conversionId?: string; // The ads accounts id used for every conversion\n currency?: string; // Default currency is EUR\n}\n\nexport type EventConfig = Mapping.EventConfig<CustomEvent>;\n\nexport interface CustomEvent {}\n","import type { Destination } from './types';\nimport { isObject } from '@elbwalker/utils';\n\n// Types\nexport * as DestinationGoogleAds from './types';\n\nexport const destinationGoogleAds: Destination = {\n type: 'google-ads',\n\n config: {},\n\n init(config = {}) {\n const { custom = {}, fn, loadScript } = config;\n const w = window;\n\n // required measurement id\n if (!custom.conversionId) return false;\n\n // Default currency value\n custom.currency = custom.currency || 'EUR';\n\n if (loadScript) addScript(custom.conversionId);\n\n w.dataLayer = w.dataLayer || [];\n\n let func = fn || w.gtag;\n if (!w.gtag) {\n w.gtag = function gtag() {\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', custom.conversionId);\n\n return config;\n },\n\n push(event, config, mapping = {}, options = {}): void {\n const { custom = {}, fn } = config;\n const { name } = mapping;\n const data = isObject(options.data) ? options.data : {};\n\n if (!name) return;\n\n const params: Gtag.CustomParams = {\n send_to: `${custom.conversionId}/${name}`,\n currency: custom.currency,\n ...data,\n };\n\n const func = fn || window.gtag;\n func('event', 'conversion', params);\n },\n};\n\nfunction addScript(\n conversionId: string,\n src = 'https://www.googletagmanager.com/gtag/js?id=',\n) {\n const script = document.createElement('script');\n script.src = src + conversionId;\n document.head.appendChild(script);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