119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Loaders for non-Google marketing/analytics pixels. Each is idempotent (guards
|
|
* against double-init) and loads the vendor's own script + bootstrap.
|
|
*
|
|
* These are the one place `any` is unavoidable: vendor SDK bootstraps attach
|
|
* untyped globals (fbq, clarity, lintrk, ttq) to `window`. Casts are contained
|
|
* here; nothing else in the kit uses `any`.
|
|
*
|
|
* CONSENT: these vendors have no Google Consent Mode — the caller (Analytics)
|
|
* loads each ONLY after the matching consent category is granted:
|
|
* Meta Pixel / LinkedIn / TikTok → marketing
|
|
* Microsoft Clarity → analytics
|
|
*/
|
|
|
|
type Win = Record<string, any> & typeof window
|
|
|
|
/** Meta (Facebook) Pixel — fires PageView on load. */
|
|
export function loadMetaPixel(id: string): void {
|
|
if (typeof window === 'undefined') {return}
|
|
const w = window as Win
|
|
if (w.fbq) {return}
|
|
const n: any = (w.fbq = function (...args: unknown[]) {
|
|
if (n.callMethod) {
|
|
n.callMethod.apply(n, args)
|
|
} else {
|
|
n.queue.push(args)
|
|
}
|
|
})
|
|
if (!w._fbq) {w._fbq = n}
|
|
n.push = n
|
|
n.loaded = true
|
|
n.version = '2.0'
|
|
n.queue = []
|
|
const s = document.createElement('script')
|
|
s.async = true
|
|
s.src = 'https://connect.facebook.net/en_US/fbevents.js'
|
|
document.head.appendChild(s)
|
|
w.fbq('init', id)
|
|
w.fbq('track', 'PageView')
|
|
}
|
|
|
|
/** Microsoft Clarity — heatmaps / session recordings (analytics category). */
|
|
export function loadClarity(id: string): void {
|
|
if (typeof window === 'undefined') {return}
|
|
const w = window as Win
|
|
if (w.clarity) {return}
|
|
w.clarity = function (...a: unknown[]) {
|
|
;(w.clarity.q = w.clarity.q || []).push(a)
|
|
}
|
|
const s = document.createElement('script')
|
|
s.async = true
|
|
s.src = `https://www.clarity.ms/tag/${id}`
|
|
document.head.appendChild(s)
|
|
}
|
|
|
|
/** LinkedIn Insight Tag (marketing). */
|
|
export function loadLinkedIn(partnerId: string): void {
|
|
if (typeof window === 'undefined') {return}
|
|
const w = window as Win
|
|
w._linkedin_partner_id = partnerId
|
|
w._linkedin_data_partner_ids = w._linkedin_data_partner_ids || []
|
|
if (w._linkedin_data_partner_ids.includes(partnerId)) {
|
|
if (document.getElementById('ipal-linkedin-insight')) {return}
|
|
} else {
|
|
w._linkedin_data_partner_ids.push(partnerId)
|
|
}
|
|
if (document.getElementById('ipal-linkedin-insight')) {return}
|
|
const s = document.createElement('script')
|
|
s.async = true
|
|
s.id = 'ipal-linkedin-insight'
|
|
s.src = 'https://snap.licdn.com/li.lms-analytics/insight.min.js'
|
|
document.head.appendChild(s)
|
|
}
|
|
|
|
/** TikTok Pixel (marketing) — fires page view on load. */
|
|
export function loadTikTok(id: string): void {
|
|
if (typeof window === 'undefined') {return}
|
|
const w = window as Win
|
|
if (w.ttq) {return}
|
|
w.TiktokAnalyticsObject = 'ttq'
|
|
const ttq: any = (w.ttq = [])
|
|
ttq.methods = [
|
|
'page',
|
|
'track',
|
|
'identify',
|
|
'instances',
|
|
'debug',
|
|
'on',
|
|
'off',
|
|
'once',
|
|
'ready',
|
|
'alias',
|
|
'group',
|
|
'enableCookie',
|
|
'disableCookie',
|
|
]
|
|
ttq.setAndDefer = function (t: any, e: string) {
|
|
t[e] = function (...args: unknown[]) {
|
|
t.push([e, ...args])
|
|
}
|
|
}
|
|
for (const m of ttq.methods) {ttq.setAndDefer(ttq, m)}
|
|
ttq.load = function (e: string) {
|
|
const url = 'https://analytics.tiktok.com/i18n/pixel/events.js'
|
|
ttq._i = ttq._i || {}
|
|
ttq._i[e] = []
|
|
ttq._i[e]._u = url
|
|
ttq._t = ttq._t || {}
|
|
ttq._t[e] = Number(new Date())
|
|
const s = document.createElement('script')
|
|
s.async = true
|
|
s.src = `${url}?sdkid=${e}&lib=ttq`
|
|
document.head.appendChild(s)
|
|
}
|
|
ttq.load(id)
|
|
ttq.page()
|
|
}
|