81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
|
|
export const GA_TRACKING_ID = import.meta.env.VITE_GA_TRACKING_ID;
|
||
|
|
export const META_PIXEL_ID = import.meta.env.VITE_META_PIXEL_ID;
|
||
|
|
|
||
|
|
// Declare global window properties for TS
|
||
|
|
declare global {
|
||
|
|
interface Window {
|
||
|
|
dataLayer: any[];
|
||
|
|
gtag: (...args: any[]) => void;
|
||
|
|
fbq: (...args: any[]) => void;
|
||
|
|
_fbq: any;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const initTracking = () => {
|
||
|
|
const isConsented = localStorage.getItem('cookie-consent') === 'accepted';
|
||
|
|
if (!isConsented) return;
|
||
|
|
|
||
|
|
// Initialize GA
|
||
|
|
if (GA_TRACKING_ID && typeof window !== 'undefined') {
|
||
|
|
const defaultDataLayer = window.dataLayer || [];
|
||
|
|
window.dataLayer = defaultDataLayer;
|
||
|
|
if (!document.getElementById('ga-script')) {
|
||
|
|
const script = document.createElement('script');
|
||
|
|
script.id = 'ga-script';
|
||
|
|
script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`;
|
||
|
|
script.async = true;
|
||
|
|
document.head.appendChild(script);
|
||
|
|
|
||
|
|
window.gtag = function gtag() {
|
||
|
|
window.dataLayer.push(arguments);
|
||
|
|
};
|
||
|
|
window.gtag('js', new Date());
|
||
|
|
window.gtag('config', GA_TRACKING_ID);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Initialize Meta Pixel
|
||
|
|
if (META_PIXEL_ID && typeof window !== 'undefined') {
|
||
|
|
!function(f:any,b:any,e:any,v:any,n:any,t:any,s:any)
|
||
|
|
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
|
||
|
|
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
|
||
|
|
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
|
||
|
|
n.queue=[];t=b.createElement(e);t.async=!0;
|
||
|
|
t.src=v;s=b.getElementsByTagName(e)[0];
|
||
|
|
if(s.parentNode) s.parentNode.insertBefore(t,s)}(window, document,'script',
|
||
|
|
'https://connect.facebook.net/en_US/fbevents.js');
|
||
|
|
window.fbq('init', META_PIXEL_ID);
|
||
|
|
window.fbq('track', 'PageView');
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const trackPageView = (url: string) => {
|
||
|
|
const isConsented = localStorage.getItem('cookie-consent') === 'accepted';
|
||
|
|
if (!isConsented) return;
|
||
|
|
|
||
|
|
if (GA_TRACKING_ID && window.gtag) {
|
||
|
|
window.gtag('config', GA_TRACKING_ID, {
|
||
|
|
page_path: url,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (META_PIXEL_ID && window.fbq) {
|
||
|
|
window.fbq('track', 'PageView');
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const trackConversion = (event_name: string) => {
|
||
|
|
const isConsented = localStorage.getItem('cookie-consent') === 'accepted';
|
||
|
|
if (!isConsented) return;
|
||
|
|
|
||
|
|
if (GA_TRACKING_ID && window.gtag) {
|
||
|
|
window.gtag('event', 'conversion', {
|
||
|
|
send_to: `${GA_TRACKING_ID}/${event_name}`
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (META_PIXEL_ID && window.fbq) {
|
||
|
|
window.fbq('trackCustom', event_name);
|
||
|
|
}
|
||
|
|
};
|