| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import type { AnalyticsClient } from './analytics.types';
- import { cleanUrl } from './privacy';
- type PrivacyNavigator = Navigator & { globalPrivacyControl?: boolean };
- /** False while rendering on the server: nothing is tracked there. */
- export function isClientAvailable(): boolean {
- return typeof window !== 'undefined' && typeof document !== 'undefined';
- }
- export function getClient(): AnalyticsClient {
- const screen = window.screen;
- return {
- platform: 'next-web',
- language: window.navigator.language || '',
- screen: screen ? `${screen.width}x${screen.height}` : '',
- };
- }
- /** The site's own origin, dropped from error sources and stack frames. */
- export function getOwnOrigin(): string {
- return window.location.origin;
- }
- /** The page an error or vitals hit belongs to: the address bar without its query string. */
- export function getHealthPageUrl(_viewUrl: string): string {
- const origin = window.location.origin;
- return origin + cleanUrl(window.location.href, origin);
- }
- /** The URL events are sent with before the first pageview. */
- export function getFallbackUrl(): string {
- return window.location.href;
- }
- /**
- * The referrer of the first page, only when it is another site: NEO's own host isn't one
- * of UNA's internal hosts, so a NEO page as referrer would count as a referral.
- */
- export function getInitialReferrer(): string {
- const referrer = document.referrer;
- if (!referrer) return '';
- try {
- return new URL(referrer).host !== window.location.host ? referrer : '';
- } catch {
- return '';
- }
- }
- /**
- * Don't track automated browsers, visitors who ask not to be tracked (Do Not Track, Global
- * Privacy Control) when the site honours it, and a browser excluded by hand with
- * `localStorage.setItem('bx_analytics_ignore', '1')`, as the classic tracker does.
- */
- export function isTrackingBlocked(respectDnt: boolean): boolean {
- const nav = window.navigator as PrivacyNavigator;
- if (nav.webdriver) return true;
- const windowDnt = (window as Window & { doNotTrack?: string }).doNotTrack;
- if (respectDnt && (nav.doNotTrack === '1' || windowDnt === '1' || nav.globalPrivacyControl === true)) return true;
- try {
- return window.localStorage.getItem('bx_analytics_ignore') === '1';
- } catch {
- return false;
- }
- }
|