@streamlayer/feature-gamification 1.8.4 → 1.9.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.
@@ -0,0 +1,15 @@
1
+ import { AdCampaigns } from '@streamlayer/sdk-web-types';
2
+ export declare const processGamAdvertisement: ({ gamOptions, gamBaseUrl }: AdCampaigns) => string;
3
+ type GamHandlers = {
4
+ onPlay?: () => void;
5
+ onStop?: () => void;
6
+ onProgress?: (opts: {
7
+ adBreakDuration: number;
8
+ adPosition: number;
9
+ currentTime: number;
10
+ duration: number;
11
+ totalAds: number;
12
+ }) => void;
13
+ };
14
+ export declare const gam: (adContainer: HTMLDivElement, url: string, { onPlay, onStop, onProgress }: GamHandlers) => () => () => void;
15
+ export {};
@@ -0,0 +1,106 @@
1
+ const encodeUrl = (urlStr) => {
2
+ const isUrl = urlStr.startsWith('http://') || urlStr.startsWith('https://');
3
+ if (!isUrl) {
4
+ return urlStr;
5
+ }
6
+ const url = new URL(urlStr);
7
+ return url.toString();
8
+ };
9
+ export const processGamAdvertisement = ({ gamOptions, gamBaseUrl }) => {
10
+ const url = `${gamBaseUrl}?`;
11
+ const params = [];
12
+ for (const rawKey in gamOptions) {
13
+ const key = rawKey.replace(/([a-zA-Z])(?=[A-Z])/g, '$1_').toLowerCase();
14
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
15
+ // @ts-expect-error
16
+ const value = gamOptions[rawKey];
17
+ if (value) {
18
+ if (typeof value === 'string') {
19
+ params.push(`${key}=${encodeUrl(value)}`);
20
+ }
21
+ else if (Array.isArray(value)) {
22
+ params.push(`${key}=${value.map(encodeUrl).join(',')}`);
23
+ }
24
+ else {
25
+ const str = Object.entries(value)
26
+ .map(([k, v]) => {
27
+ if (Array.isArray(v)) {
28
+ return `${k}=${v.map(encodeUrl).join(',')}`;
29
+ }
30
+ if (typeof v === 'string') {
31
+ return `${k}=${encodeUrl(v)}`;
32
+ }
33
+ return `${k}=${v}`;
34
+ })
35
+ .join('&')
36
+ .replaceAll('=', '%3D')
37
+ .replaceAll('&', '%26')
38
+ .replaceAll(',', '%2C');
39
+ params.push(`${key}=${str}`);
40
+ }
41
+ }
42
+ }
43
+ return url + params.join('&');
44
+ };
45
+ export const gam = (adContainer, url, { onPlay, onStop, onProgress }) => {
46
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
47
+ // @ts-expect-error
48
+ const ima = google.ima;
49
+ let adDisplayContainer = new ima.AdDisplayContainer(adContainer);
50
+ let adsLoader = new ima.AdsLoader(adDisplayContainer);
51
+ let adsRequest = new ima.AdsRequest();
52
+ let adsManager;
53
+ adsLoader.addEventListener(ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, (adsManagerLoadedEvent) => {
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ adsManager = adsManagerLoadedEvent.getAdsManager(adContainer);
56
+ if (onPlay) {
57
+ adsManager.addEventListener(ima.AdEvent.Type.STARTED, onPlay);
58
+ }
59
+ if (onStop) {
60
+ adsManager.addEventListener(ima.AdEvent.Type.COMPLETE, onStop);
61
+ }
62
+ if (onProgress) {
63
+ adsManager.addEventListener(ima.AdEvent.Type.AD_PROGRESS, (d) => onProgress(d.getAdData()));
64
+ }
65
+ adsManager.init(adContainer.clientWidth, adContainer.clientHeight, ima.ViewMode.NORMAL);
66
+ adsManager.start();
67
+ }, false);
68
+ adsLoader.addEventListener(ima.AdErrorEvent.Type.AD_ERROR, () => {
69
+ console.log('ad error');
70
+ onStop?.();
71
+ }, false);
72
+ adsRequest.linearAdSlotWidth = adContainer.clientWidth;
73
+ adsRequest.linearAdSlotHeight = adContainer.clientHeight;
74
+ // adsRequest.nonLinearAdSlotWidth = adContainer.clientWidth ??
75
+ // adsRequest.nonLinearAdSlotHeight = adContainer.clientHeight / 3 ??
76
+ adsRequest.adTagUrl = url;
77
+ adsLoader.requestAds(adsRequest);
78
+ const destroy = () => {
79
+ adsManager?.destroy();
80
+ adsLoader?.destroy();
81
+ adDisplayContainer?.destroy();
82
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
83
+ // @ts-expect-error
84
+ adDisplayContainer = undefined;
85
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
86
+ // @ts-expect-error
87
+ adsLoader = undefined;
88
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
89
+ // @ts-expect-error
90
+ adsRequest = undefined;
91
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
92
+ // @ts-expect-error
93
+ adsManager = undefined;
94
+ };
95
+ const play = () => {
96
+ try {
97
+ adDisplayContainer.initialize();
98
+ }
99
+ catch (adError) {
100
+ onStop?.();
101
+ console.log('AdsManager could not be started', adError);
102
+ }
103
+ return destroy;
104
+ };
105
+ return play;
106
+ };
@@ -4,6 +4,10 @@ import { getPromotionDetail } from '../queries';
4
4
  type AdvertisementData = Exclude<Awaited<ReturnType<typeof getPromotionDetail>>, undefined>;
5
5
  export type Advertisement = {
6
6
  data?: AdvertisementData;
7
+ externalAd?: {
8
+ type: 'gam';
9
+ url: string;
10
+ };
7
11
  loading?: boolean;
8
12
  hiding?: boolean;
9
13
  isViewed?: boolean;
@@ -1,10 +1,33 @@
1
1
  import { createMapStore, eventBus } from '@streamlayer/sdk-web-interfaces';
2
2
  import { QuestionStatus } from '@streamlayer/sdk-web-types';
3
3
  import { createLogger } from '@streamlayer/sdk-web-logger';
4
- import { NotificationEnabled } from '@streamlayer/sl-eslib/interactive/interactive.common_pb';
4
+ import { NotificationEnabled, PromotionType } from '@streamlayer/sl-eslib/interactive/interactive.common_pb';
5
5
  import { $activePromotionId, getPromotionDetail } from '../queries';
6
6
  import { AdvertisementStorage } from './storage';
7
7
  import { parsePromotion } from './utils';
8
+ import { processGamAdvertisement } from './externalAd';
9
+ // async function generateNonce() {
10
+ // const request = new goog.pal.NonceRequest()
11
+ // request.adWillAutoPlay = true
12
+ // request.adWillPlayMuted = false
13
+ // request.continuousPlayback = false
14
+ // request.descriptionUrl = 'https://example.com'
15
+ // request.iconsSupported = true
16
+ // request.playerType = 'Sample Player Type'
17
+ // request.playerVersion = '1.0'
18
+ // request.ppid = 'Sample PPID'
19
+ // request.sessionId = 'Sample SID'
20
+ // // Player support for VPAID 2.0, OMID 1.0, and SIMID 1.1
21
+ // request.supportedApiFrameworks = '2,7,9'
22
+ // request.url = 'https://developers.google.com/ad-manager/pal/html5'
23
+ // request.videoHeight = 480
24
+ // request.videoWidth = 640
25
+ // const consentSettings = new goog.pal.ConsentSettings()
26
+ // consentSettings.allowStorage = true
27
+ // const nonceLoader = new goog.pal.NonceLoader(consentSettings)
28
+ // const manager = await nonceLoader.loadNonceManager(request)
29
+ // return manager.getNonce()
30
+ // }
8
31
  /**
9
32
  * @name Advertisement
10
33
  * @description advertisement functionality, show, hide
@@ -90,13 +113,19 @@ export const advertisement = ($slStreamId, $feedSubscription, instance) => {
90
113
  }
91
114
  };
92
115
  $store.subscribe((active, prevActive) => {
116
+ if (active.data?.promotion?.type === PromotionType.EXTERNAL_AD && !active?.externalAd) {
117
+ if (active.data.promotion.adCampaigns?.gamOptions && active.data.promotion.adCampaigns?.gamBaseUrl) {
118
+ const gamUrl = processGamAdvertisement(active.data.promotion.adCampaigns);
119
+ $store.setKey('externalAd', { type: 'gam', url: gamUrl });
120
+ }
121
+ }
93
122
  // skip update on open
94
- if (active.data && !active.isOpened) {
123
+ if (active.data?.promotion && !active.isOpened) {
95
124
  eventBus.emit('advertisement', {
96
125
  action: 'received',
97
126
  payload: {
98
127
  id: active.data.question.id,
99
- type: active.data?.promotion?.type,
128
+ type: active.data.promotion.type,
100
129
  hasNotification: !!active.hasNotification,
101
130
  isViewed: !!storage.isViewed(active.data.question.id),
102
131
  },
package/lib/deepLink.js CHANGED
@@ -13,8 +13,8 @@ export const deepLink = (transport, $eventId, $externalEventId, $userId) => {
13
13
  const $store = createMapStore({});
14
14
  onMount($store, () => {
15
15
  const $branchLink = $deepLink(transport, [$eventId, $externalEventId]);
16
- const cancel = $branchLink.subscribe(async ({ data, error, loading }) => {
17
- const mobileDeepLink = error ? 'https://masters.com' : data?.url;
16
+ const cancel = $branchLink.subscribe(async ({ data, loading }) => {
17
+ const mobileDeepLink = data === undefined && loading === false ? 'https://streamlayer.io' : data?.url;
18
18
  const userId = $userId.get() || '';
19
19
  const eventId = $eventId.get() || '';
20
20
  const externalEventId = $externalEventId.get() || '';
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@streamlayer/feature-gamification",
3
- "version": "1.8.4",
3
+ "version": "1.9.1",
4
4
  "peerDependencies": {
5
5
  "@bufbuild/protobuf": "^1.10.0",
6
6
  "@fastify/deepmerge": "^2.0.0",
7
7
  "@streamlayer/sl-eslib": "^5.130.0",
8
8
  "nanostores": "^0.10.3",
9
- "@streamlayer/sdk-web-api": "^1.6.5",
10
- "@streamlayer/sdk-web-core": "^1.6.4",
11
- "@streamlayer/sdk-web-interfaces": "^1.2.5",
12
- "@streamlayer/sdk-web-logger": "^1.0.26",
13
- "@streamlayer/sdk-web-notifications": "^1.2.5",
14
- "@streamlayer/sdk-web-storage": "^1.0.26",
15
- "@streamlayer/sdk-web-types": "^1.7.5"
9
+ "@streamlayer/sdk-web-api": "^1.6.7",
10
+ "@streamlayer/sdk-web-core": "^1.6.6",
11
+ "@streamlayer/sdk-web-interfaces": "^1.2.7",
12
+ "@streamlayer/sdk-web-logger": "^1.0.28",
13
+ "@streamlayer/sdk-web-notifications": "^1.2.7",
14
+ "@streamlayer/sdk-web-storage": "^1.0.28",
15
+ "@streamlayer/sdk-web-types": "^1.8.1"
16
16
  },
17
17
  "devDependencies": {
18
18
  "tslib": "^2.7.0"
@@ -31,6 +31,11 @@
31
31
  "require": "./lib/gamification.js",
32
32
  "types": "./lib/gamification.d.ts"
33
33
  },
34
+ "./externalAd": {
35
+ "module": "./lib/advertisement/externalAd/index.js",
36
+ "require": "./lib/advertisement/externalAd/index.js",
37
+ "types": "./lib/advertisement/externalAd/index.d.ts"
38
+ },
34
39
  "./highlights": {
35
40
  "module": "./lib/highlights.js",
36
41
  "require": "./lib/highlights.js",