'use client' import { components } from 'api-types' import { useRouter } from 'next/compat/router' import { usePathname } from 'next/navigation' import Script from 'next/script' import { useCallback, useEffect, useRef, useState } from 'react' import { useLatest } from 'react-use' import { useUser } from './auth' import { hasConsented, useConsentState } from './consent-state' import { createConsentedUrlCookieSync } from './consented-url-cookie' import { IS_PLATFORM } from './constants' import { useFeatureFlags } from './feature-flags' import { post } from './fetchWrappers' import type { FirstReferrerData, MwDiagData } from './first-referrer-cookie' import { isExternalReferrer, isOAuthRedirectReferrer, parseFirstReferrerCookie, parseMwDiagCookie, } from './first-referrer-cookie' import { ensurePlatformSuffix, isBrowser } from './helpers' import { useFirstTouchStore, useParams } from './hooks' import { buildSessionRecordingConfig, posthogClient, type CapturedNetworkRequest, type ClientTelemetryEvent, type SessionRecordingOptions, } from './posthog-client' import { TelemetryEvent } from './telemetry-constants' import { clearFirstTouchData, getFirstTouchData, type SharedTelemetryData, } from './telemetry-first-touch-store' import { getSharedTelemetryData, getTelemetryCookieOptions } from './telemetry-utils' export { buildSessionRecordingConfig, posthogClient, type CapturedNetworkRequest, type ClientTelemetryEvent, type SessionRecordingOptions, } export const TelemetryTagManager = () => { const { hasAccepted } = useConsentState() const [syncCookie] = useState(createConsentedUrlCookieSync) const isGTMEnabled = Boolean( IS_PLATFORM && process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID && hasAccepted ) // Ad attribution capture is independent of GTM, so this runs even when the // container is not configured. useEffect(() => { syncCookie(hasAccepted) }, [hasAccepted, syncCookie]) if (!isGTMEnabled) return null return (