@playpilot/tpi 8.21.8 → 8.21.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playpilot/tpi",
3
- "version": "8.21.8",
3
+ "version": "8.21.9",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -17,7 +17,7 @@
17
17
  "postinstall": "patch-package"
18
18
  },
19
19
  "devDependencies": {
20
- "@playpilot/retargeting-tracking": "^1.4.1",
20
+ "@playpilot/retargeting-tracking": "^1.4.2",
21
21
  "@sveltejs/adapter-auto": "^7.0.1",
22
22
  "@sveltejs/kit": "^2.56.1",
23
23
  "@sveltejs/vite-plugin-svelte": "^4.0.0",
@@ -4,7 +4,7 @@ import { isCrawler } from './crawler'
4
4
  import { genreSlugsToNames } from './genre'
5
5
  import type { TitleData } from './types/title'
6
6
  import { getFullUrlPath } from './url'
7
- import { getUserId, isUserTrackingAllowed } from './user'
7
+ import { getSessionId, getUserId, isUserTrackingAllowed } from './user'
8
8
 
9
9
  const baseUrl = 'https://insights.playpilot.net'
10
10
 
@@ -41,6 +41,7 @@ export async function track(event: string, title: TitleData | null = null, paylo
41
41
 
42
42
  if (isUserTrackingAllowed()) {
43
43
  payload.user_id = getUserId()
44
+ payload.session_id = getSessionId()
44
45
  payload.region = window.PlayPilotLinkInjections.region
45
46
  }
46
47
 
package/src/lib/user.ts CHANGED
@@ -2,6 +2,7 @@ import { hasConsentedTo } from './consent'
2
2
  import { generateRandomHash } from './hash'
3
3
 
4
4
  export const localStorageUserKey = 'playpilot_user_id'
5
+ export const sessionStorageSessionKey = 'playpilot_session_id'
5
6
 
6
7
  export function getUserId(): string | null {
7
8
  if (!isUserTrackingAllowed()) return null
@@ -17,6 +18,20 @@ export function getUserId(): string | null {
17
18
  }
18
19
  }
19
20
 
21
+ export function getSessionId(): string | null {
22
+ if (!isUserTrackingAllowed()) return null
23
+
24
+ try {
25
+ const sessionId = sessionStorage.getItem(sessionStorageSessionKey)
26
+ if (sessionId) return sessionId
27
+
28
+ sessionStorage.setItem(sessionStorageSessionKey, generateRandomHash(16))
29
+ return sessionStorage.getItem(sessionStorageSessionKey)
30
+ } catch {
31
+ return null
32
+ }
33
+ }
34
+
20
35
  export function isUserTrackingAllowed(): boolean {
21
36
  return !!window.PlayPilotLinkInjections?.config?.allow_user_id_tracking && hasConsentedTo('tracking')
22
37
  }
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect, beforeEach, vi } from 'vitest'
2
- import { getUserId, isUserTrackingAllowed, localStorageUserKey } from '$lib/user'
2
+ import { getSessionId, getUserId, isUserTrackingAllowed, localStorageUserKey, sessionStorageSessionKey } from '$lib/user'
3
3
  import { hasConsentedTo } from '$lib/consent'
4
4
 
5
5
  vi.mock('$lib/consent', () => ({
@@ -45,6 +45,44 @@ describe('user.ts', () => {
45
45
  })
46
46
  })
47
47
 
48
+ describe('getSessionId', () => {
49
+ beforeEach(() => {
50
+ // @ts-ignore
51
+ window.PlayPilotLinkInjections = {
52
+ config: {
53
+ allow_user_id_tracking: true,
54
+ },
55
+ }
56
+
57
+ vi.mocked(hasConsentedTo).mockImplementation(() => true)
58
+ })
59
+
60
+ it('Should return random hash by default', () => {
61
+ expect(getSessionId()).toBeTruthy()
62
+ })
63
+
64
+ it('Should return the previously set value', () => {
65
+ sessionStorage.setItem(sessionStorageSessionKey, 'some_value')
66
+
67
+ expect(getSessionId()).toBe('some_value')
68
+ })
69
+
70
+ it('Should return null if allow_user_id_tracking is not set', () => {
71
+ // @ts-ignore
72
+ window.PlayPilotLinkInjections = {}
73
+
74
+ expect(getSessionId()).toBe(null)
75
+
76
+ sessionStorage.setItem(sessionStorageSessionKey, 'some_value')
77
+
78
+ expect(getSessionId()).toBe(null)
79
+ })
80
+
81
+ it('Should return the same id when called multiple times', () => {
82
+ expect(getSessionId()).toBe(getSessionId())
83
+ })
84
+ })
85
+
48
86
  describe('isUserTrackingAllowed', () => {
49
87
  beforeEach(() => {
50
88
  // @ts-ignore
@@ -96,7 +96,9 @@ beforeEach(() => {
96
96
  document.cookie.split(';').forEach((cookie) => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, '=;'))
97
97
 
98
98
  window.HTMLElement.prototype.scrollIntoView = vi.fn()
99
+
99
100
  localStorage.clear()
101
+ sessionStorage.clear()
100
102
 
101
103
  mockAnimations()
102
104
  })