File size: 2,803 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"use client";

type DataFastValue =
  | string
  | number
  | boolean
  | readonly (string | number | boolean)[]
  | null
  | undefined;

export type DataFastMetadata = Record<string, DataFastValue>;

type DataFastIdentifyPayload = DataFastMetadata & {
  user_id: string;
};

declare global {
  interface Window {
    datafast?: {
      (...args: [string] | [string, Record<string, string>]): void;
      q?: IArguments[];
    };
    // Tilly analytics, running alongside DataFast (see layout.tsx).
    tilly?: {
      (goal: string, metadata?: Record<string, string>): void;
      q?: IArguments[];
    };
  }
}

const dataFastWebsiteId = process.env.NEXT_PUBLIC_DATAFAST_WEBSITE_ID;
const dataFastDomain = process.env.NEXT_PUBLIC_DATAFAST_DOMAIN;

const isClient = () => typeof window !== "undefined";

export const isDataFastConfigured = Boolean(dataFastWebsiteId && dataFastDomain);

const sanitizeKey = (key: string) =>
  key
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9_-]/g, "_")
    .slice(0, 64);

const sanitizeValue = (value: DataFastValue) => {
  if (value === null || value === undefined) {
    return null;
  }

  return String(value).slice(0, 255);
};

const sanitizeMetadata = (metadata?: DataFastMetadata) => {
  if (!metadata) {
    return undefined;
  }

  const sanitizedEntries = Object.entries(metadata)
    .map(([key, value]) => {
      const sanitizedKey = sanitizeKey(key);
      const sanitizedValue = sanitizeValue(value);

      if (!sanitizedKey || sanitizedValue === null) {
        return null;
      }

      return [sanitizedKey, sanitizedValue] as const;
    })
    .filter((entry): entry is readonly [string, string] => entry !== null)
    .slice(0, 10);

  if (sanitizedEntries.length === 0) {
    return undefined;
  }

  return Object.fromEntries(sanitizedEntries);
};

const sanitizeGoalName = (goalName: string) =>
  goalName
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9_-]/g, "_")
    .slice(0, 64);

export function track(goalName: string, metadata?: DataFastMetadata) {
  if (!isClient()) {
    return;
  }

  const sanitizedGoalName = sanitizeGoalName(goalName);
  if (!sanitizedGoalName) {
    return;
  }

  const sanitizedMetadata = sanitizeMetadata(metadata);
  window.tilly?.(sanitizedGoalName, sanitizedMetadata);

  if (!isDataFastConfigured || !window.datafast) {
    return;
  }

  if (sanitizedMetadata) {
    window.datafast(sanitizedGoalName, sanitizedMetadata);
    return;
  }

  window.datafast(sanitizedGoalName);
}

export function identify(metadata: DataFastIdentifyPayload) {
  if (!isClient() || !isDataFastConfigured || !window.datafast) {
    return;
  }

  const sanitizedMetadata = sanitizeMetadata(metadata);
  if (!sanitizedMetadata?.user_id) {
    return;
  }

  window.datafast("identify", sanitizedMetadata);
}