From 47be0ed3bc9e594f584daa30bba09e349140e8f5 Mon Sep 17 00:00:00 2001 From: Nick Santaniello Date: Mon, 8 Apr 2024 13:33:29 -0700 Subject: [PATCH] Restored tracking pixel helper methods for js-util --- packages/util/src/index.ts | 1 + packages/util/src/tracking-pixel.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 packages/util/src/tracking-pixel.ts diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 7f02f3bf..b786ccfa 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -13,4 +13,5 @@ export { } from './gif-utils' export * from './log' export * from './sdk-headers' +export { default as injectTrackingPixel } from './tracking-pixel' export { checkIfWebP } from './webp-check' diff --git a/packages/util/src/tracking-pixel.ts b/packages/util/src/tracking-pixel.ts new file mode 100644 index 00000000..b241fe12 --- /dev/null +++ b/packages/util/src/tracking-pixel.ts @@ -0,0 +1,15 @@ +import { sanitize } from 'dompurify' + +const injectTrackingPixel = (tags: string[] = []) => { + tags.forEach((tag) => { + const el = document.createElement('html') + tag = tag.replace('%%CACHEBUSTER%%', Date.now().toString()) + el.innerHTML = sanitize(tag) + const pixel = el.querySelector('img') + if (pixel) { + document?.querySelector('head')?.appendChild(pixel) + } + }) +} + +export default injectTrackingPixel