@playpilot/tpi 8.21.8 → 8.22.0-beta.1

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.22.0-beta.1",
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",
@@ -7,9 +7,10 @@ type Options = {
7
7
  headers?: Record<string, string>
8
8
  method?: 'POST' | 'GET'
9
9
  body?: null | Record<string, any>
10
+ baseUrl?: string
10
11
  }
11
12
 
12
- export async function api<T>(path: string, { headers = {}, method = 'GET', body = null }: Options = {}): Promise<T> {
13
+ export async function api<T>(path: string, { headers = {}, method = 'GET', body = null, baseUrl = apiBaseUrl }: Options = {}): Promise<T> {
13
14
  // For GET methods we store all results in a cache object. This cache is super simple and has no invalidation at all.
14
15
  // Once something is stored it's there forever since the page is only short lived.
15
16
  // We never use the cache for editiorial mode since in the editor requests do change.
@@ -18,7 +19,7 @@ export async function api<T>(path: string, { headers = {}, method = 'GET', body
18
19
 
19
20
  const baseHeaders: Record<string, string> = method === 'GET' ? {} : { 'Content-Type': 'application/json' }
20
21
 
21
- const response = await fetch(apiBaseUrl + path, {
22
+ const response = await fetch(baseUrl + path, {
22
23
  method,
23
24
  headers: new Headers({ ...baseHeaders, ...headers }),
24
25
  // eslint-disable-next-line no-undef
@@ -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
 
@@ -1 +1 @@
1
- export type LanguageCode = 'en-US' | 'sv-SE' | 'dk-DK'
1
+ export type LanguageCode = 'en-US' | 'sv-SE' | 'da-DK'
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
  }
@@ -2,37 +2,23 @@
2
2
  import type { TitleData } from '$lib/types/title'
3
3
  import { fetchTitles } from '$lib/api/titles'
4
4
  import { t } from '$lib/localization'
5
- import { Sorting } from '$lib/enums/Sorting'
6
5
  import { track } from '$lib/tracking'
7
6
  import { TrackingEvent } from '$lib/enums/TrackingEvent'
8
7
  import TitlesRail from '../../Rails/TitlesRail.svelte'
9
8
  import { mobileBreakpoint } from '$lib/constants'
9
+ import { getLanguage } from '$lib/language'
10
+ import type { LanguageCode } from '$lib/types/language'
11
+ import { api } from '$lib/api/api'
10
12
 
11
13
  let expandedTitle: TitleData | null = $state(null)
12
14
  let expandedRailKey: string | null = $state(null)
13
15
  let windowWidth: number = $state(window.innerWidth)
14
16
 
15
- const rails: { heading: string, params: Record<string, any>, properties: Record<string, any> }[] = [{
16
- heading: 'List: Upcoming',
17
- params: { from_playlist_sid: 'li42wf', region: null, no_region_filter: true },
18
- properties: { expandable: true },
19
- }, {
20
- heading: 'List: Trending',
21
- params: { ordering: Sorting.Popular },
22
- properties: { expandable: true },
23
- }, {
24
- heading: 'List: New',
25
- params: { from_playlist_sid: 'li42WR', include_playable_types: 'SVOD,FREE' },
26
- properties: { aside: true, size: 'flexible' },
27
- }, {
28
- heading: 'List: Cinema',
29
- params: { from_playlist_sid: 'li42WS', region: null, no_region_filter: true },
30
- properties: { expandable: true },
31
- }, {
32
- heading: 'List: Demand',
33
- params: { from_playlist_sid: 'licBS' },
34
- properties: { aside: true, size: 'flexible' },
35
- }]
17
+ type Rail = { headings: Record<LanguageCode, string>, params: Record<string, any>, properties: Record<string, any> }
18
+
19
+ async function fetchRails(): Promise<Rail[]> {
20
+ return await api<Rail[]>('/streaming-guide/rails', { baseUrl: 'https://partner.playpilot.net/api' })
21
+ }
36
22
 
37
23
  async function getListTitles(params: Record<string, any> = {}): Promise<TitleData[]> {
38
24
  try {
@@ -49,20 +35,28 @@
49
35
 
50
36
  <div data-testid="explore-home"></div>
51
37
 
52
- {#each rails as { heading, params, properties }}
53
- <div class="rail">
54
- <TitlesRail
55
- heading={t(heading)}
56
- titles={getListTitles(params)}
57
- size={windowWidth >= mobileBreakpoint ? 'flexible' : 'huge'}
58
- minimumLength={7}
59
- {...properties}
60
- bind:expandedTitle
61
- bind:expandedRailKey
62
- onclick={() => track(TrackingEvent.ExploreTitleRailModalView, null, { rail: heading })}
63
- onexpand={(title) => track(TrackingEvent.ExploreTitleRailExpandClick, title, { rail: heading })} />
64
- </div>
65
- {/each}
38
+ {#await fetchRails()}
39
+ <TitlesRail titles={new Promise((res) => setTimeout(res, 10000))} size={windowWidth >= mobileBreakpoint ? 'flexible' : 'huge'} />
40
+ {:then rails}
41
+ {#each rails as { headings, params, properties }}
42
+ {@const heading = headings[getLanguage()]}
43
+
44
+ <div class="rail">
45
+ <TitlesRail
46
+ {heading}
47
+ titles={getListTitles(params)}
48
+ size={windowWidth >= mobileBreakpoint ? (properties.size || 'flexible') : 'huge'}
49
+ minimumLength={7}
50
+ {...properties}
51
+ bind:expandedTitle
52
+ bind:expandedRailKey
53
+ onclick={() => track(TrackingEvent.ExploreTitleRailModalView, null, { rail: headings })}
54
+ onexpand={(title) => track(TrackingEvent.ExploreTitleRailExpandClick, title, { rail: headings })} />
55
+ </div>
56
+ {/each}
57
+ {:catch}
58
+ {t('An Error Occurred')}
59
+ {/await}
66
60
 
67
61
  <style lang="scss">
68
62
  .rail {
@@ -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
@@ -1,23 +1,34 @@
1
- import { render } from '@testing-library/svelte'
1
+ import { render, waitFor } from '@testing-library/svelte'
2
2
  import { beforeEach, describe, expect, it, vi } from 'vitest'
3
3
 
4
4
  import ExploreHome from '../../../../../routes/components/Explore/Routes/ExploreHome.svelte'
5
5
  import { fetchTitles } from '$lib/api/titles'
6
+ import { api } from '$lib/api/api'
6
7
 
7
8
  vi.mock('$lib/api/titles', () => ({
8
9
  fetchTitles: vi.fn(),
9
10
  }))
10
11
 
12
+ vi.mock('$lib/api/api', () => ({
13
+ api: vi.fn(),
14
+ }))
15
+
11
16
  describe('ExploreResults.svelte', () => {
12
17
  beforeEach(() => {
13
18
  vi.resetAllMocks()
14
19
  })
15
20
 
16
- it('Should fetch titles for each rail', () => {
21
+ it('Should fetch rails endpoint and titles for each rail', async () => {
22
+ const rail = { headings: {}, properties: {}, params: {} }
23
+
24
+ vi.mocked(api).mockResolvedValue([rail, rail, rail, rail, rail])
17
25
  vi.mocked(fetchTitles).mockResolvedValue({ next: null, previous: null, results: [] })
18
26
 
19
27
  render(ExploreHome)
20
28
 
21
- expect(fetchTitles).toHaveBeenCalledTimes(5)
29
+ await waitFor(() => {
30
+ expect(api).toHaveBeenCalledOnce()
31
+ expect(fetchTitles).toHaveBeenCalledTimes(5)
32
+ })
22
33
  })
23
34
  })
@@ -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
  })