UNPKG

@elbwalker/destination-web-piwikpro

Version:

Piwik PRO destination for walkerOS

1 lines 4.16 kB
{"version":3,"sources":["../src/index.ts","../src/types/index.ts"],"sourcesContent":["import type { Custom, CustomEvent, Destination } from './types';\nimport { getMappingValue, isArray } from '@elbwalker/utils';\n\n// Types\nexport * as DestinationPiwikPro from './types';\n\nexport const destinationPiwikPro: Destination = {\n type: 'piwikpro',\n\n config: {},\n\n init(config = {}) {\n const w = window;\n const { custom = {} as Partial<Custom>, fn, loadScript } = config;\n const { appId, url } = custom;\n\n // Required parameters\n if (!appId || !url) return false;\n\n // Set up the Piwik Pro interface _paq\n w._paq = w._paq || [];\n\n const func = fn || w._paq.push;\n if (loadScript) {\n // Load the JavaScript Tracking Client\n addScript(url);\n\n // Register the tracker url only with script loading\n func(['setTrackerUrl', url + 'ppms.php']);\n\n // Register app id\n func(['setSiteId', appId]);\n }\n\n // Enable download and outlink tracking if not disabled\n if (custom.linkTracking !== false) func(['enableLinkTracking']);\n },\n\n async push(event, config, mapping = {}, options = {}) {\n const { fn } = config;\n const { data } = options;\n const func = fn || window._paq!.push;\n\n // Send pageviews if not disabled\n if (event.event === 'page view' && !mapping.custom) {\n func(['trackPageView', await getMappingValue(event, 'data.title')]);\n return;\n }\n\n const customMapping: CustomEvent = mapping.custom || {};\n\n const parameters = isArray(data) ? data : [data];\n\n func([event.event, ...parameters]);\n\n if (customMapping.goalId) {\n const goalValue = customMapping.goalValue\n ? getMappingValue(event, customMapping.goalValue)\n : undefined;\n\n func([\n 'trackGoal',\n customMapping.goalId,\n goalValue,\n // @TODO dimensions\n ]);\n }\n },\n};\n\nfunction addScript(url: string) {\n const script = document.createElement('script');\n script.type = 'text/javascript';\n script.src = url + 'ppms.js';\n script.async = true;\n script.defer = true;\n document.head.appendChild(script);\n}\n\nexport default destinationPiwikPro;\n","import type { Mapping } from '@elbwalker/types';\nimport type { DestinationWeb } from '@elbwalker/walker.js';\n\ndeclare global {\n interface Window {\n _paq?: Array<unknown>;\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 appId: string;\n // dimensions?: Dimensions;\n linkTracking?: boolean;\n url: string;\n}\n\nexport interface CustomEvent {\n // dimensions?: Dimensions;\n goalId?: string;\n goalValue?: string;\n}\n\nexport type EventConfig = Mapping.EventConfig<CustomEvent>;\n\nexport interface Dimensions {\n [i: number]: string;\n}\n"],"mappings":";AACA,SAAS,iBAAiB,eAAe;;;ACDzC;;;ADMO,IAAM,sBAAmC;AAAA,EAC9C,MAAM;AAAA,EAEN,QAAQ,CAAC;AAAA,EAET,KAAK,SAAS,CAAC,GAAG;AAChB,UAAM,IAAI;AACV,UAAM,EAAE,SAAS,CAAC,GAAsB,IAAI,WAAW,IAAI;AAC3D,UAAM,EAAE,OAAO,IAAI,IAAI;AAGvB,QAAI,CAAC,SAAS,CAAC,IAAK,QAAO;AAG3B,MAAE,OAAO,EAAE,QAAQ,CAAC;AAEpB,UAAM,OAAO,MAAM,EAAE,KAAK;AAC1B,QAAI,YAAY;AAEd,gBAAU,GAAG;AAGb,WAAK,CAAC,iBAAiB,MAAM,UAAU,CAAC;AAGxC,WAAK,CAAC,aAAa,KAAK,CAAC;AAAA,IAC3B;AAGA,QAAI,OAAO,iBAAiB,MAAO,MAAK,CAAC,oBAAoB,CAAC;AAAA,EAChE;AAAA,EAEA,MAAM,KAAK,OAAO,QAAQ,UAAU,CAAC,GAAG,UAAU,CAAC,GAAG;AACpD,UAAM,EAAE,GAAG,IAAI;AACf,UAAM,EAAE,KAAK,IAAI;AACjB,UAAM,OAAO,MAAM,OAAO,KAAM;AAGhC,QAAI,MAAM,UAAU,eAAe,CAAC,QAAQ,QAAQ;AAClD,WAAK,CAAC,iBAAiB,MAAM,gBAAgB,OAAO,YAAY,CAAC,CAAC;AAClE;AAAA,IACF;AAEA,UAAM,gBAA6B,QAAQ,UAAU,CAAC;AAEtD,UAAM,aA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