@splendidlabz/tracking 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,372 @@
1
+ import { isEmpty } from '@splendidlabz/utils'
2
+ import { sha256Hash } from '@splendidlabz/utils/dom'
3
+
4
+ export async function formatUserData(data) {
5
+ if (isEmpty(data)) return {}
6
+
7
+ // Lowercase all the values
8
+ for (const [key, value] of Object.entries(data)) {
9
+ data[key] = value.toLowerCase()
10
+ }
11
+
12
+ const {
13
+ email,
14
+ phone,
15
+ firstName,
16
+ lastName,
17
+ birthdate,
18
+ gender,
19
+ city,
20
+ state,
21
+ zip,
22
+ country,
23
+ } = data
24
+
25
+ const ret = {}
26
+
27
+ if (email) ret.em = await sha256Hash(email)
28
+ if (phone) ret.ph = await sha256Hash(phone)
29
+ if (firstName) ret.fn = await sha256Hash(firstName)
30
+ if (lastName) ret.ln = await sha256Hash(lastName)
31
+ if (birthdate) ret.db = await sha256Hash(birthdate)
32
+ if (gender) ret.ge = await sha256Hash(gender)
33
+ if (city) ret.ct = await sha256Hash(city.replace(' ', ''))
34
+
35
+ if (country) {
36
+ const c = COUNTRY_CODES[country]
37
+ ret.country = await sha256Hash(c)
38
+
39
+ if (c === 'us' && state) {
40
+ ret.st = await sha256Hash(US_STATE_CODES[state])
41
+ }
42
+ if (c !== 'us' && state) {
43
+ ret.st = await sha256Hash(state.replace(' ', '').replace('-', ''))
44
+ }
45
+ }
46
+ if (zip) ret.zp = await sha256Hash(zip)
47
+ return ret
48
+ }
49
+
50
+ const US_STATE_CODES = {
51
+ alabama: 'al',
52
+ alaska: 'ak',
53
+ 'american samoa': 'as',
54
+ arizona: 'az',
55
+ arkansas: 'ar',
56
+ 'baker island': 'bi',
57
+ california: 'ca',
58
+ colorado: 'co',
59
+ connecticut: 'ct',
60
+ delaware: 'de',
61
+ 'district of columbia': 'dc',
62
+ florida: 'fl',
63
+ 'federated states of micronesia': 'fm',
64
+ georgia: 'ga',
65
+ guam: 'gu',
66
+ hawaii: 'hi',
67
+ 'howland island': 'hi',
68
+ idaho: 'id',
69
+ illinois: 'il',
70
+ indiana: 'in',
71
+ iowa: 'ia',
72
+ 'jarvis island': 'ji',
73
+ 'johnston atoll': 'ja',
74
+ kansas: 'ks',
75
+ kentucky: 'ky',
76
+ 'kingman reef': 'kr',
77
+ louisiana: 'la',
78
+ maine: 'me',
79
+ 'marshall islands': 'mh',
80
+ maryland: 'md',
81
+ massachusetts: 'ma',
82
+ michigan: 'mi',
83
+ 'midway islands': 'mi',
84
+ minnesota: 'mn',
85
+ mississippi: 'ms',
86
+ missouri: 'mo',
87
+ montana: 'mt',
88
+ 'navassa island': 'ni',
89
+ nebraska: 'ne',
90
+ nevada: 'nv',
91
+ 'new hampshire': 'nh',
92
+ 'new jersey': 'nj',
93
+ 'new mexico': 'nm',
94
+ 'new york': 'ny',
95
+ 'north carolina': 'nc',
96
+ 'north dakota': 'nd',
97
+ 'northern mariana islands': 'mp',
98
+ ohio: 'oh',
99
+ oklahoma: 'ok',
100
+ oregon: 'or',
101
+ palau: 'pw',
102
+ 'palmyra atoll': 'pa',
103
+ pennsylvania: 'pa',
104
+ 'puerto rico': 'pr',
105
+ 'rhode island': 'ri',
106
+ 'south carolina': 'sc',
107
+ 'south dakota': 'sd',
108
+ tennessee: 'tn',
109
+ texas: 'tx',
110
+ 'u.s. minor outlying islands': 'um',
111
+ utah: 'ut',
112
+ vermont: 'vt',
113
+ virginia: 'va',
114
+ 'virgin islands of the u.s.': 'vi',
115
+ 'wake island': 'wi',
116
+ washington: 'wa',
117
+ 'west virginia': 'wv',
118
+ wisconsin: 'wi',
119
+ wyoming: 'wy',
120
+ }
121
+
122
+ const COUNTRY_CODES = {
123
+ afghanistan: 'af',
124
+ 'åland islands': 'ax',
125
+ albania: 'al',
126
+ algeria: 'dz',
127
+ 'american samoa': 'as',
128
+ andorra: 'ad',
129
+ angola: 'ao',
130
+ anguilla: 'ai',
131
+ antarctica: 'aq',
132
+ 'antigua and barbuda': 'ag',
133
+ argentina: 'ar',
134
+ armenia: 'am',
135
+ aruba: 'aw',
136
+ australia: 'au',
137
+ austria: 'at',
138
+ azerbaijan: 'az',
139
+ bahamas: 'bs',
140
+ bahrain: 'bh',
141
+ bangladesh: 'bd',
142
+ barbados: 'bb',
143
+ belarus: 'by',
144
+ belgium: 'be',
145
+ belize: 'bz',
146
+ benin: 'bj',
147
+ bermuda: 'bm',
148
+ bhutan: 'bt',
149
+ 'bolivia (plurinational state of)': 'bo',
150
+ 'bonaire, sint eustatius and saba': 'bq',
151
+ 'bosnia and herzegovina': 'ba',
152
+ botswana: 'bw',
153
+ 'bouvet island': 'bv',
154
+ brazil: 'br',
155
+ 'british indian ocean territory': 'io',
156
+ 'brunei darussalam': 'bn',
157
+ brunei: 'bn',
158
+ bulgaria: 'bg',
159
+ 'burkina faso': 'bf',
160
+ burundi: 'bi',
161
+ 'cabo verde': 'cv',
162
+ cambodia: 'kh',
163
+ cameroon: 'cm',
164
+ canada: 'ca',
165
+ 'cayman islands': 'ky',
166
+ 'central african republic': 'cf',
167
+ chad: 'td',
168
+ chile: 'cl',
169
+ china: 'cn',
170
+ 'christmas island': 'cx',
171
+ 'cocos (keeling) islands': 'cc',
172
+ colombia: 'co',
173
+ comoros: 'km',
174
+ 'democratic republic of the congo': 'cd',
175
+ 'cook islands': 'ck',
176
+ 'costa rica': 'cr',
177
+ "côte d'ivoire": 'ci',
178
+ croatia: 'hr',
179
+ cuba: 'cu',
180
+ curaçao: 'cw',
181
+ cyprus: 'cy',
182
+ czechia: 'cz',
183
+ denmark: 'dk',
184
+ djibouti: 'dj',
185
+ dominica: 'dm',
186
+ 'dominican republic': 'do',
187
+ ecuador: 'ec',
188
+ egypt: 'eg',
189
+ 'el salvador': 'sv',
190
+ 'equatorial guinea': 'gq',
191
+ eritrea: 'er',
192
+ estonia: 'ee',
193
+ eswatini: 'sz',
194
+ ethiopia: 'et',
195
+ 'falkland islands': 'fk',
196
+ 'faroe islands': 'fo',
197
+ fiji: 'fj',
198
+ finland: 'fi',
199
+ france: 'fr',
200
+ 'french guiana': 'gf',
201
+ 'french polynesia': 'pf',
202
+ 'french southern territories': 'tf',
203
+ gabon: 'ga',
204
+ gambia: 'gm',
205
+ georgia: 'ge',
206
+ germany: 'de',
207
+ ghana: 'gh',
208
+ gibraltar: 'gi',
209
+ greece: 'gr',
210
+ greenland: 'gl',
211
+ grenada: 'gd',
212
+ guadeloupe: 'gp',
213
+ guam: 'gu',
214
+ guatemala: 'gt',
215
+ guernsey: 'gg',
216
+ guinea: 'gn',
217
+ 'guinea-bissau': 'gw',
218
+ guyana: 'gy',
219
+ haiti: 'ht',
220
+ 'heard island and mcdonald islands': 'hm',
221
+ 'holy see': 'va',
222
+ honduras: 'hn',
223
+ 'hong kong': 'hk',
224
+ hungary: 'hu',
225
+ iceland: 'is',
226
+ india: 'in',
227
+ indonesia: 'id',
228
+ iran: 'ir',
229
+ iraq: 'iq',
230
+ ireland: 'ie',
231
+ 'isle of man': 'im',
232
+ israel: 'il',
233
+ italy: 'it',
234
+ jamaica: 'jm',
235
+ japan: 'jp',
236
+ jersey: 'je',
237
+ jordan: 'jo',
238
+ kazakhstan: 'kz',
239
+ kenya: 'ke',
240
+ kiribati: 'ki',
241
+ 'north korea': 'kp',
242
+ 'south korea': 'kr',
243
+ kuwait: 'kw',
244
+ kyrgyzstan: 'kg',
245
+ laos: 'la',
246
+ latvia: 'lv',
247
+ lebanon: 'lb',
248
+ lesotho: 'ls',
249
+ liberia: 'lr',
250
+ libya: 'ly',
251
+ liechtenstein: 'li',
252
+ lithuania: 'lt',
253
+ luxembourg: 'lu',
254
+ macao: 'mo',
255
+ madagascar: 'mg',
256
+ malawi: 'mw',
257
+ malaysia: 'my',
258
+ maldives: 'mv',
259
+ mali: 'ml',
260
+ malta: 'mt',
261
+ 'marshall islands': 'mh',
262
+ martinique: 'mq',
263
+ mauritania: 'mr',
264
+ mauritius: 'mu',
265
+ mayotte: 'yt',
266
+ mexico: 'mx',
267
+ micronesia: 'fm',
268
+ moldova: 'md',
269
+ monaco: 'mc',
270
+ mongolia: 'mn',
271
+ montenegro: 'me',
272
+ montserrat: 'ms',
273
+ morocco: 'ma',
274
+ mozambique: 'mz',
275
+ myanmar: 'mm',
276
+ namibia: 'na',
277
+ nauru: 'nr',
278
+ nepal: 'np',
279
+ netherlands: 'nl',
280
+ 'new caledonia': 'nc',
281
+ 'new zealand': 'nz',
282
+ nicaragua: 'ni',
283
+ niger: 'ne',
284
+ nigeria: 'ng',
285
+ niue: 'nu',
286
+ 'norfolk island': 'nf',
287
+ 'north macedonia': 'mk',
288
+ 'northern mariana islands': 'mp',
289
+ norway: 'no',
290
+ oman: 'om',
291
+ pakistan: 'pk',
292
+ palau: 'pw',
293
+ palestine: 'ps',
294
+ panama: 'pa',
295
+ 'papua new guinea': 'pg',
296
+ paraguay: 'py',
297
+ peru: 'pe',
298
+ philippines: 'ph',
299
+ pitcairn: 'pn',
300
+ poland: 'pl',
301
+ portugal: 'pt',
302
+ 'puerto rico': 'pr',
303
+ qatar: 'qa',
304
+ réunion: 're',
305
+ romania: 'ro',
306
+ 'russian federation': 'ru',
307
+ rwanda: 'rw',
308
+ 'saint barthélemy': 'bl',
309
+ 'saint helena': 'sh',
310
+ 'saint kitts and nevis': 'kn',
311
+ 'saint lucia': 'lc',
312
+ 'saint martin': 'mf',
313
+ 'saint pierre and miquelon': 'pm',
314
+ 'saint vincent and the grenadines': 'vc',
315
+ samoa: 'ws',
316
+ 'san marino': 'sm',
317
+ 'sao tome and principe': 'st',
318
+ 'saudi arabia': 'sa',
319
+ senegal: 'sn',
320
+ serbia: 'rs',
321
+ seychelles: 'sc',
322
+ 'sierra leone': 'sl',
323
+ singapore: 'sg',
324
+ 'sint maarten': 'sx',
325
+ slovakia: 'sk',
326
+ slovenia: 'si',
327
+ 'solomon islands': 'sb',
328
+ somalia: 'so',
329
+ 'south africa': 'za',
330
+ 'south georgia and the south sandwich islands': 'gs',
331
+ 'south sudan': 'ss',
332
+ spain: 'es',
333
+ 'sri lanka': 'lk',
334
+ sudan: 'sd',
335
+ suriname: 'sr',
336
+ 'svalbard and jan mayen': 'sj',
337
+ sweden: 'se',
338
+ switzerland: 'ch',
339
+ 'syrian arab republic': 'sy',
340
+ taiwan: 'tw',
341
+ tajikistan: 'tj',
342
+ tanzania: 'tz',
343
+ thailand: 'th',
344
+ 'timor-leste': 'tl',
345
+ togo: 'tg',
346
+ tokelau: 'tk',
347
+ tonga: 'to',
348
+ 'trinidad and tobago': 'tt',
349
+ tunisia: 'tn',
350
+ türkiye: 'tr',
351
+ turkmenistan: 'tm',
352
+ 'turks and caicos islands': 'tc',
353
+ tuvalu: 'tv',
354
+ uganda: 'ug',
355
+ ukraine: 'ua',
356
+ 'united arab emirates': 'ae',
357
+ 'united kingdom': 'gb',
358
+ 'united states minor outlying islands': 'um',
359
+ 'united states': 'us',
360
+ uruguay: 'uy',
361
+ uzbekistan: 'uz',
362
+ vanuatu: 'vu',
363
+ venezuela: 've',
364
+ 'viet nam': 'vn',
365
+ 'british virgin islands': 'vg',
366
+ 'u.s. virgin islands': 'vi',
367
+ 'wallis and futuna': 'wf',
368
+ 'western sahara': 'eh',
369
+ yemen: 'ye',
370
+ zambia: 'zm',
371
+ zimbabwe: 'zw',
372
+ }
@@ -0,0 +1,10 @@
1
+ <script>
2
+ import { tracking } from './cross-domain'
3
+ import { onMount } from 'svelte'
4
+ export let domains = []
5
+ const rest = { ...$$restProps }
6
+
7
+ onMount(_ => {
8
+ tracking(domains)
9
+ })
10
+ </script>
@@ -0,0 +1,50 @@
1
+ import { cookies, localStore } from '@splendidlabz/utils/dom'
2
+
3
+ import posthog from 'posthog-js'
4
+
5
+ function PH(apiKey, options) {
6
+ posthog.init(apiKey, options)
7
+
8
+ return {
9
+ getDistinctID() {
10
+ const key = `ph_${apiKey}_posthog`
11
+ return localStore.get(key) || cookies.get(key)
12
+ },
13
+
14
+ crossDomainTracking(domains) {
15
+ // Converts domains into array
16
+ if (typeof domains === 'string') domains = [domains]
17
+
18
+ // Adds phid to the link before sending.
19
+ document.addEventListener('click', event => {
20
+ const link = event.target.closest('a')
21
+ if (link === null) return
22
+
23
+ // Checks if we should add the posthogID to the URL
24
+ let href = link.getAttribute('href')
25
+ const shouldModify = domains.some(domain => href.includes(domain))
26
+ if (!shouldModify) return
27
+
28
+ // If phid is already in the URL, skip
29
+ const url = new URL(href)
30
+ if (url.searchParams.has('phid')) return
31
+
32
+ // Append phid to the URL
33
+ const posthogID = this.getDistinctID()
34
+ url.searchParams.append('phid', posthogID)
35
+ link.href = url.href
36
+ })
37
+ },
38
+ }
39
+ }
40
+
41
+ export function identify() {
42
+ const url = new URL(window.location.href)
43
+ const phid = url.searchParams.get('phid')
44
+
45
+ if (phid) {
46
+ posthog.identify(phid)
47
+ url.searchParams.delete('phid')
48
+ window.history.replaceState({}, '', url.href)
49
+ }
50
+ }
@@ -0,0 +1 @@
1
+ export * from './cross-domain.js'
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@splendidlabz/tracking",
3
+ "version": "0.1.20",
4
+ "description": "Simple Tracking Helper",
5
+ "type": "module",
6
+ "files": [
7
+ "dist",
8
+ "lib/fb",
9
+ "lib/posthog",
10
+ "lib/convertkit"
11
+ ],
12
+ "exports": {
13
+ "./*": "./lib/*"
14
+ },
15
+ "scripts": {
16
+ "test": "vitest",
17
+ "test:watch": "vitest --watch",
18
+ "build": "rollup -c",
19
+ "release": "npm run build && np"
20
+ },
21
+ "author": "Zell Liew <zellwk@gmail.com> (https://zellwk.com/)",
22
+ "dependencies": {
23
+ "@splendidlabz/utils": "*",
24
+ "posthog-js": "^1.114.2",
25
+ "posthog-node": "^3.6.3",
26
+ "zl-fetch": "^6.0.2"
27
+ },
28
+ "devDependencies": {
29
+ "@rollup/plugin-node-resolve": "^15.2.3",
30
+ "@rollup/plugin-terser": "^0.4.4",
31
+ "jsdom": "^24.0.0",
32
+ "np": "^10.0.0",
33
+ "rollup": "^4.12.0",
34
+ "rollup-plugin-commonjs": "^10.1.0",
35
+ "rollup-plugin-filesize": "^10.0.0",
36
+ "vitest": "^1.3.1"
37
+ }
38
+ }