import { Partytown } from "@qwik.dev/partytown/react";
import * as React from "react";
import { useLocation } from "react-router";
import { useRouteContext } from "../../contexts/RouteContext";
// type dataLayer
declare global {
interface Window {
dataLayer: {
push(...args: any[]): void;
};
}
}
function resolveUrl(
url: URL,
location: Location,
type: "script" | "style" | "iframe" | "fetch" | "image" | "xhr",
) {
if (type !== "script") {
return url;
}
const bypassDomains = [
"www.google-analytics.com",
"www.googletagmanager.com",
"cdn.mxpnl.com",
"unpkg.com",
"cdnjs.cloudflare.com",
"esm.sh",
"cors-proxy.devize.com",
];
if (bypassDomains.includes(url.hostname)) {
return url;
}
const proxyUrl = new URL("https://cors-proxy.devize.com/");
proxyUrl.searchParams.append("url", url.toString());
return proxyUrl;
}
export const AnalyticsHead = () => {
const {
settings: { analytics: settings },
} = useRouteContext();
const hasGTM = settings?.gtm?.enabled;
const hasGA = settings?.ga?.enabled;
const hasMP = settings?.mp?.enabled;
const hasPH = settings?.ph?.enabled;
const partyTownEnabled = hasGTM || hasGA || hasMP;
const forward: string[] = [];
if (hasGTM || hasGA) {
forward.push("dataLayer.push");
}
if (hasMP) {
forward.push(
"mixpanel.identify",
"mixpanel.people.set",
"mixpanel.track",
"mixpanel.reset",
"mixpanel.alias",
);
}
return (
<>
{partyTownEnabled ? (
) : null}
{hasGTM ? : null}
{hasGA ? : null}
{hasMP ? : null}
{hasPH ? (
) : null}
>
);
};
export const AnalyticsBody = () => {
const {
settings: { analytics: settings },
} = useRouteContext();
const mId = settings?.ga?.mId;
const location = useLocation();
React.useEffect(() => {
window.dataLayer?.push(["config", mId, { page_path: location.pathname }]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [location]);
const hasGTM = settings?.gtm?.enabled;
return hasGTM ? (
) : null;
};
const GoogleTagManager = ({ containerId }) => {
return containerId ? (
) : null;
};
const GoogleTagManagerNoScript = ({ containerId }) => {
return containerId ? (
) : null;
};
const GoogleAnalytics = ({ measurementId }) => {
return measurementId ? (
<>
>
) : null;
};
const Mixpanel = ({ token }) => {
return token ? (
) : null;
};
const PostHog = ({
apikey,
host = "https://us.i.posthog.com",
}: {
apikey: string;
host: string;
}) => {
return apikey ? (
) : null;
};