@schibsted/pulse-sdk 8.0.0-rc.5 → 8.0.0-rc.7

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.
Files changed (37) hide show
  1. package/dist/cjs/Tracker.js +137 -119
  2. package/dist/cjs/leaveTrackingTpaas/constants.js +2 -7
  3. package/dist/cjs/leaveTrackingTpaas/index.js +52 -40
  4. package/dist/cjs/tracker-proxy/consts.js +3 -0
  5. package/dist/cjs/version.js +1 -1
  6. package/dist/cjs/visibility-observer/index.js +9 -7
  7. package/dist/cjs/wrapper/types/event.js +5 -0
  8. package/dist/ejs/Tracker.js +138 -120
  9. package/dist/ejs/leaveTrackingTpaas/constants.js +1 -5
  10. package/dist/ejs/leaveTrackingTpaas/index.js +53 -41
  11. package/dist/ejs/tracker-proxy/consts.js +3 -0
  12. package/dist/ejs/version.js +1 -1
  13. package/dist/ejs/visibility-observer/index.js +9 -7
  14. package/dist/ejs/wrapper/types/event.js +4 -1
  15. package/dist/types/Tracker.d.ts +55 -20
  16. package/dist/types/index.d.ts +1 -1
  17. package/dist/types/leaveTrackingTpaas/constants.d.ts +1 -5
  18. package/dist/types/leaveTrackingTpaas/index.d.ts +6 -6
  19. package/dist/types/leaveTrackingTpaas/types.d.ts +1 -9
  20. package/dist/types/tracker-proxy/consts.d.ts +1 -1
  21. package/dist/types/types.d.ts +19 -13
  22. package/dist/types/version.d.ts +1 -1
  23. package/dist/types/visibility-observer/index.d.ts +6 -1
  24. package/dist/types/wrapper/types/event.d.ts +3 -1
  25. package/node_modules/@schibsted/pulse-utils/package.json +1 -1
  26. package/node_modules/@schibsted/tpaas-event-builder/dist/cjs/builders.js +5 -1
  27. package/node_modules/@schibsted/tpaas-event-builder/dist/cjs/guards.js +7 -0
  28. package/node_modules/@schibsted/tpaas-event-builder/dist/cjs/index.js +3 -0
  29. package/node_modules/@schibsted/tpaas-event-builder/dist/ejs/builders.js +5 -2
  30. package/node_modules/@schibsted/tpaas-event-builder/dist/ejs/guards.js +4 -0
  31. package/node_modules/@schibsted/tpaas-event-builder/dist/ejs/index.js +1 -0
  32. package/node_modules/@schibsted/tpaas-event-builder/dist/index.d.ts +792 -166
  33. package/node_modules/@schibsted/tpaas-event-builder/package.json +1 -1
  34. package/node_modules/@schibsted/tpaas-schemas/dist/cjs/constants.js +66 -57
  35. package/node_modules/@schibsted/tpaas-schemas/dist/ejs/constants.js +64 -55
  36. package/node_modules/@schibsted/tpaas-schemas/dist/index.d.ts +836 -219
  37. package/package.json +4 -4
@@ -1,6 +1,6 @@
1
1
  import { isBrowser, isFunction, throttle } from '@schibsted/pulse-utils';
2
2
  import * as logger from 'loglevel';
3
- import { DEFAULT_ACTIVITY_DURATION, DEFAULT_PAGE_LEAVE_EVENT } from './constants';
3
+ import { ACTIVITY_DURATION_LIMIT_MS, DEFAULT_PAGE_LEAVE_EVENT } from './constants';
4
4
  import { BrowserEvents } from './types';
5
5
  let currentLeaveSchema;
6
6
  let currentTrackMethod;
@@ -9,19 +9,42 @@ let updateViewedContentListeners = [];
9
9
  let activityListeners = [];
10
10
  let resizeObserver = null;
11
11
  let throttledUpdateFn = throttle(() => undefined, 0);
12
- let startTime = 0;
13
- let activityDuration = DEFAULT_ACTIVITY_DURATION();
12
+ let durationStartTime = 0;
13
+ let activityStartTime = 0;
14
14
  let currentLeaveEvent = DEFAULT_PAGE_LEAVE_EVENT();
15
15
  let additionalHeightBuilder;
16
+ /**
17
+ * Resolves a DOM element from either a CSS selector string or an Element instance.
18
+ */
19
+ function resolveDomElement(element) {
20
+ try {
21
+ return typeof element === 'string' ? document.querySelector(element) : element || null;
22
+ }
23
+ catch (e) {
24
+ logger.error('Error while resolving DOM element', e);
25
+ return null;
26
+ }
27
+ }
16
28
  /*
17
29
  Add leave tracking
18
30
  */
19
31
  export function addTpaasLeaveTracking({ objectElement, pageElement, schema, objectResizable, heightBuilder, trackMethod, }) {
20
32
  function _addLeaveTracking() {
33
+ const objectDomElement = resolveDomElement(objectElement);
34
+ if (objectDomElement == null) {
35
+ logger.error('Could not find object element for leave tracking');
36
+ return;
37
+ }
38
+ let pageDomElement = resolveDomElement(pageElement);
39
+ if (pageDomElement == null) {
40
+ logger.warn('Could not find page element for leave tracking, defaulting to document.body');
41
+ pageDomElement = document.body;
42
+ }
21
43
  additionalHeightBuilder = heightBuilder;
22
44
  addTpaasLeaveListeners();
23
- addUpdateEventListeners(objectElement, pageElement || document.body, objectResizable);
24
- startTime = resetTime();
45
+ addUpdateEventListeners(objectDomElement, pageDomElement, objectResizable);
46
+ activityStartTime = resetTime();
47
+ durationStartTime = resetTime();
25
48
  currentLeaveSchema = schema;
26
49
  currentTrackMethod = trackMethod;
27
50
  logger.debug('addLeaveTracking:after:state', currentLeaveEvent);
@@ -113,12 +136,8 @@ function removeUpdateEventListeners() {
113
136
  */
114
137
  const resetEventDefaults = () => {
115
138
  currentLeaveEvent = DEFAULT_PAGE_LEAVE_EVENT();
116
- startTime = resetTime();
117
- activityDuration = resetActivityDuration();
118
- };
119
- const resetActivityDuration = () => {
120
- clearInterval(activityDuration.interval);
121
- return DEFAULT_ACTIVITY_DURATION();
139
+ durationStartTime = resetTime();
140
+ activityStartTime = resetTime();
122
141
  };
123
142
  export const resetTpaasViewedContent = () => {
124
143
  currentLeaveEvent = DEFAULT_PAGE_LEAVE_EVENT();
@@ -126,34 +145,41 @@ export const resetTpaasViewedContent = () => {
126
145
  const resetTime = () => Date.now();
127
146
  /*
128
147
  Send a Leave event only if Page Leave tracking is active in the SDK
129
- The event that is ultimately sent is a merge between the default event
130
- saved in this file and the event supplied by the brand, with preference going
131
- to the brand event. After the event has been sent, leave page tracking is removed
148
+ The event that is ultimately sent is the View event saved in the currentTrackMethod callback.
149
+ After the event has been sent, leave page tracking is removed
132
150
  and must be explicitly re-added if needed.
133
151
  */
134
- export function trackTpaasActiveLeave() {
152
+ export async function trackTpaasActiveLeave() {
135
153
  if (leaveTpaasTrackingIsActive()) {
136
- trackTpaasLeave();
154
+ await trackTpaasLeave();
137
155
  }
138
156
  }
139
157
  /*
140
158
  Send a Leave event regardless of whether Page Leave tracking is active in the SDK
141
- The event that is ultimately sent is a merge between the default event
142
- saved in this file and the event supplied by the brand, with preference going
143
- to the brand event. After the event has been sent, leave page tracking is removed
159
+ The event that is ultimately sent is the View event saved in the currentTrackMethod callback.
160
+ After the event has been sent, leave page tracking is removed
144
161
  and must be explicitly re-added if needed.
145
162
  */
146
- export function trackTpaasLeave() {
163
+ export async function trackTpaasLeave() {
147
164
  const leave = getTpaasLeaveObject();
148
- currentTrackMethod(leave);
165
+ // Capture currentTrackMethod and remove tracking synchronously before awaiting.
166
+ // If removal happened after the await, a new trackView* call arriving during the async
167
+ // gap would see tracking still active with the same schema and silently discard its
168
+ // new callback (e.g. a fresh leaveAudioBuilder), causing the next leave event to
169
+ // never fire or to use the wrong builder.
170
+ const trackMethod = currentTrackMethod;
149
171
  removeTpaasLeaveTracking();
172
+ await trackMethod(leave);
150
173
  }
151
174
  export function getTpaasLeaveObject() {
152
175
  const endTime = Date.now();
153
- const duration = endTime - startTime;
176
+ // TPaaS leave event requires duration/activity to be at least 1ms, otherwise the event is rejected
177
+ // also, in practice, it's almost impossible to have an activity or duration of 0ms
178
+ const durationMs = Math.max(endTime - durationStartTime, 1);
179
+ const activityDurationMs = Math.max(Math.min(endTime - activityStartTime, ACTIVITY_DURATION_LIMIT_MS), 1);
154
180
  const leave = {
155
- durationMs: duration,
156
- activityDurationMs: activityDuration.value,
181
+ durationMs,
182
+ activityDurationMs,
157
183
  ...currentLeaveEvent,
158
184
  };
159
185
  logger.debug('trackTpaasLeave: event', leave);
@@ -226,18 +252,10 @@ export const calculateScrollPosition = (element) => Math.abs(Math.min(element.ge
226
252
  'mousedown', 'mousemove', 'keydown', 'scroll' and/or 'touchstart' browser events.
227
253
  */
228
254
  function addUpdateActivityDurationListeners() {
229
- if (activityDuration.interval !== undefined) {
230
- // If the interval is already set, do not set it again
255
+ // if there are already listeners for activity, do not add more
256
+ if (activityListeners.length > 0) {
231
257
  return;
232
258
  }
233
- const interval = 1000;
234
- const activityTimeout = 30 * 1000;
235
- activityDuration.interval = window.setInterval(() => {
236
- activityDuration.timeSinceLastActivity += interval;
237
- if (activityDuration.timeSinceLastActivity < activityTimeout) {
238
- activityDuration.value += interval;
239
- }
240
- }, interval);
241
259
  const activityEvents = [
242
260
  BrowserEvents.SCROLL,
243
261
  BrowserEvents.MOUSEDOWN,
@@ -246,15 +264,9 @@ function addUpdateActivityDurationListeners() {
246
264
  BrowserEvents.TOUCHSTART,
247
265
  ];
248
266
  activityListeners = addEventListeners(activityEvents, () => {
249
- activityDuration = resetLastActivity(activityDuration);
267
+ activityStartTime = resetTime();
250
268
  }, { passive: true });
251
269
  }
252
- function resetLastActivity(activityDuration) {
253
- return {
254
- ...activityDuration,
255
- timeSinceLastActivity: 0,
256
- };
257
- }
258
270
  /*
259
271
  Add listeners to a set of browser events.
260
272
  */
@@ -18,6 +18,7 @@ export const TPAAS_ALLOWED_METHODS = [
18
18
  'trackEngagementVideoAd',
19
19
  'trackEngagementWidget',
20
20
  'trackCompletedHealthAction',
21
+ 'trackImpressionAdSlot',
21
22
  'trackImpressionForm',
22
23
  'trackImpressionHealthUIElement',
23
24
  'trackImpressionOffer',
@@ -35,6 +36,7 @@ export const TPAAS_ALLOWED_METHODS = [
35
36
  'trackLeaveLockedAudioPage',
36
37
  'trackLeaveLockedVideoPage',
37
38
  'trackLeavePage',
39
+ 'trackLeaveSportsPage',
38
40
  'trackLeaveVideoPage',
39
41
  'trackLeaveWeather',
40
42
  'trackViewArticle',
@@ -48,6 +50,7 @@ export const TPAAS_ALLOWED_METHODS = [
48
50
  'trackViewLockedAudioPage',
49
51
  'trackViewLockedVideoPage',
50
52
  'trackViewPage',
53
+ 'trackViewSportsPage',
51
54
  'trackViewTitle',
52
55
  'trackViewVideoPage',
53
56
  'trackViewWeather',
@@ -1,2 +1,2 @@
1
1
  // This file is auto-generated by bin/generate-version.js. Do not edit manually.
2
- export const SDK_VERSION = '8.0.0-rc.5';
2
+ export const SDK_VERSION = '8.0.0-rc.7';
@@ -13,14 +13,16 @@ export class VisibilityObserver {
13
13
  * Uses {@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | MutationObserver} to monitor the DOM for dynamically loaded content.
14
14
  * Callback is invoked only once per element.
15
15
  * @param selector The CSS selector for elements to track.
16
+ * @param options {ObserverOptions} Optional configuration for the observer.
17
+ * @param options.threshold Which ratio triggers the Observer callback.
16
18
  * @throws Error if called more than once.
17
19
  */
18
- observe(selector) {
20
+ observe(selector, { threshold = 0.67 }) {
19
21
  if (this.selector) {
20
22
  throw new Error(`Cannot observe selector ${selector}. A visibility observer has already been set up for selector ${this.selector}.`);
21
23
  }
22
24
  this.selector = selector;
23
- this.setupObservers();
25
+ this.setupObservers({ threshold });
24
26
  // biome-ignore lint/suspicious/useIterableCallbackReturn: -
25
27
  this.elements().forEach((elementToTrack) => this.observeIntersection(elementToTrack));
26
28
  }
@@ -30,14 +32,14 @@ export class VisibilityObserver {
30
32
  elements() {
31
33
  return Array.from(document.querySelectorAll(this.selector));
32
34
  }
33
- // Initialize both observers and start watching for DOM changes
34
- setupObservers() {
35
- this.setupIntersectionObserver();
35
+ // Initialise both observers and start watching for DOM changes
36
+ setupObservers({ threshold }) {
37
+ this.setupIntersectionObserver({ threshold });
36
38
  this.setupMutationObserver();
37
39
  this.observeMutations();
38
40
  }
39
41
  // Create observer to detect when elements become visible in the viewport
40
- setupIntersectionObserver() {
42
+ setupIntersectionObserver({ threshold }) {
41
43
  this.intersectionObserver = new IntersectionObserver((entries) => {
42
44
  entries.forEach((entry) => {
43
45
  if (entry.isIntersecting) {
@@ -46,7 +48,7 @@ export class VisibilityObserver {
46
48
  this.intersectionObserver.unobserve(node);
47
49
  }
48
50
  });
49
- }, { threshold: 2 / 3 });
51
+ }, { threshold });
50
52
  }
51
53
  // Create observer to detect DOM changes that may add/remove tracked elements
52
54
  setupMutationObserver() {
@@ -1 +1,4 @@
1
- export {};
1
+ const LEGACY_PAGE_TYPE_VALUES = ['Article', 'Listing', 'Frontpage', 'SalesPoster', 'Page', 'LoginPoster'];
2
+ export function isLegacyPageType(value) {
3
+ return LEGACY_PAGE_TYPE_VALUES.includes(value);
4
+ }
@@ -1,6 +1,6 @@
1
1
  import { type AdvertisingIdentifiers, type AdvertisingVendor } from '@schibsted/pulse-cis-sync';
2
2
  import { getCommonBuildersOutput, type InputParams } from '@schibsted/tpaas-event-builder';
3
- import { type IAnonymousViewUIElement, type ICompletedHealthAction, type IEngagementAudio, type IEngagementForm, type IEngagementHealthUIElement, type IEngagementOffer, type IEngagementTeaser, type IEngagementUIElement, type IEngagementVideo, type IEngagementVideoAd, type IEngagementWidget, type IImpressionForm, type IImpressionHealthUIElement, type IImpressionOffer, type IImpressionPlayer, type IImpressionTeaser, type IImpressionUIElement, type IImpressionWidget, type ILeave, type ILeaveArticle, type ILeaveAudioPage, type ILeaveError, type ILeaveFrontpage, type ILeaveLandingpage, type ILeaveListing, type ILeaveLockedArticle, type ILeaveLockedAudioPage, type ILeaveLockedVideoPage, type ILeavePage, type ILeaveSportsPage, type ILeaveVideoPage, type ILeaveWeather, type ITpaasEvent, type IViewArticle, type IViewAudioPage, type IViewError, type IViewFrontpage, type IViewHealthPage, type IViewLandingpage, type IViewListing, type IViewLockedArticle, type IViewLockedAudioPage, type IViewLockedVideoPage, type IViewPage, type IViewSportsPage, type IViewTitle, type IViewVideoPage, type IViewWeather } from '@schibsted/tpaas-schemas';
3
+ import { type IAnonymousViewUIElement, type ICompletedHealthAction, type IEngagementAudio, type IEngagementForm, type IEngagementHealthUIElement, type IEngagementOffer, type IEngagementTeaser, type IEngagementUIElement, type IEngagementVideo, type IEngagementVideoAd, type IEngagementWidget, type IImpressionAdSlot, type IImpressionForm, type IImpressionHealthUIElement, type IImpressionOffer, type IImpressionPlayer, type IImpressionTeaser, type IImpressionUIElement, type IImpressionWidget, type ILeave, type ILeaveArticle, type ILeaveAudioPage, type ILeaveError, type ILeaveFrontpage, type ILeaveLandingpage, type ILeaveListing, type ILeaveLockedArticle, type ILeaveLockedAudioPage, type ILeaveLockedVideoPage, type ILeavePage, type ILeaveSportsPage, type ILeaveVideoPage, type ILeaveWeather, type ITpaasEvent, type IViewArticle, type IViewAudioPage, type IViewError, type IViewFrontpage, type IViewHealthPage, type IViewLandingpage, type IViewListing, type IViewLockedArticle, type IViewLockedAudioPage, type IViewLockedVideoPage, type IViewPage, type IViewSportsPage, type IViewTitle, type IViewVideoPage, type IViewWeather } from '@schibsted/tpaas-schemas';
4
4
  import logger from 'loglevel';
5
5
  import type { ActorInput, Consents, ConsentsInput, DeviceInput, LocationInput, ObjectInput, OriginInput, ProviderInput, TrackerInput } from './builders';
6
6
  import { engagementEvent, identityEvent, routableEvent, trackerEvent } from './builders/events';
@@ -8,9 +8,10 @@ import { anonymousEngagementEvent, anonymousTrackerEvent } from './builders/even
8
8
  import type { Deferred, ResolvedInput } from './defer';
9
9
  import { type Listener } from './EventBus/EventBus';
10
10
  import type { LeaveEvent, LeaveEventBuilder, LeaveEventOptions } from './leaveTracking/types';
11
- import type { AnonymousEventInputTypesByTrackingMethod, EventInput, EventInputTypesByTrackingMethod, TrackViewAudioPageOptions, TrackViewEventOptions, TrackViewVideoPageOptions } from './types';
11
+ import type { AnonymousEventInputTypesByTrackingMethod, EventInput, EventInputTypesByTrackingMethod, TrackViewEventOptions } from './types';
12
+ import { type ObserverOptions } from './visibility-observer';
12
13
  import type { ClickUIElementInputProperties } from './wrapper/engagement/ui-element/types';
13
- import type { BaseEventExperiment } from './wrapper/types';
14
+ import { type BaseEventExperiment } from './wrapper/types';
14
15
  import type { ViewArticleInputProperties } from './wrapper/view/article/types';
15
16
  import type { ViewFrontpageInputProperties } from './wrapper/view/frontpage/types';
16
17
  import type { ViewListingInputProperties } from './wrapper/view/listing/types';
@@ -273,8 +274,16 @@ export default class Tracker {
273
274
  private bookmark;
274
275
  private legacyVisibilityObservers;
275
276
  private visibilityObservers;
276
- private legacyCurrentPage;
277
+ /**
278
+ * @private
279
+ *
280
+ * This is used as the source of truth for the current page's information, which is updated based on the View Page-level event
281
+ * As a single source of truth for the current page, this needs to be compatible between legacy and TPaaS events, hence the need for a custom data type.
282
+ * Legacy events can provide an id with SDRN, while TPaaS events don't need it
283
+ */
277
284
  private currentPage;
285
+ private getCurrentTpaasPage;
286
+ private getCurrentLegacyPage;
278
287
  /**
279
288
  * Promise that resolves when a session ID becomes available
280
289
  * @private
@@ -376,7 +385,6 @@ export default class Tracker {
376
385
  */
377
386
  private prepareAnonymousTpaasEvent;
378
387
  /**
379
- *
380
388
  * @private
381
389
  * @category TPaaS Tracking
382
390
  */
@@ -461,6 +469,28 @@ export default class Tracker {
461
469
  * @category Tracking
462
470
  */
463
471
  addUIElementVisibilityTracking(selector: string, buildEventFromElement: (element: HTMLElement) => InputParams<IImpressionUIElement>): void;
472
+ /**
473
+ * The `addElementVisibilityObserver` function provides an interface for executing arbitrary code when specific DOM Elements become visible within the viewport.
474
+ * It is built on the `VisibilityObserver` class, which efficiently detects visibility changes in DOM elements.
475
+ * When an element becomes at least 2/3 visible, a callback is triggered, with the Element that invoked the callback call as argument.
476
+ * It leverages the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for viewport tracking
477
+ * and the [Mutation Observer API](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to monitor lazy-loaded elements.
478
+ *
479
+ * Example usage:
480
+ *
481
+ * ```ts
482
+ * tracker.addElementVisibilityObserver('[data-pulse-entity-id]', (element) => {
483
+ * const eventInput = parseElementIntoEventInput(element); // user-defined function
484
+ * tracker.trackImpressionTeaser(eventInput); // can be anything actually, not necessarily a Teaser tracking call
485
+ * });
486
+ * ```
487
+ * @param selector
488
+ * @param callback
489
+ * @param options {ObserverOptions}
490
+ * @param options.threshold - A number between 0 and 1 indicating how much of the element should be visible to trigger
491
+ * the callback. Default is 0.67 (2/3 of the element).
492
+ */
493
+ addElementVisibilityObserver(selector: string, callback: (element: HTMLElement) => void, options?: ObserverOptions): void;
464
494
  private setPageDefaults;
465
495
  /**
466
496
  * The old trackViewArticle method from the Media Wrapper.
@@ -549,20 +579,20 @@ export default class Tracker {
549
579
  private setCurrentPage;
550
580
  trackCompletedHealthAction(input: InputParams<ICompletedHealthAction>): Promise<unknown>;
551
581
  trackEngagementHealthUIElement(input: InputParams<IEngagementHealthUIElement>): Promise<unknown>;
552
- trackViewHealthPage(input: InputParams<IViewHealthPage>, options: TrackViewEventOptions): Promise<unknown>;
582
+ trackViewHealthPage(input: InputParams<IViewHealthPage>, options: TrackViewEventOptions<IViewHealthPage>): Promise<unknown>;
553
583
  trackImpressionHealthUIElement(input: InputParams<IImpressionHealthUIElement>): Promise<unknown>;
554
584
  /**
555
585
  * @param input
556
586
  * @param options
557
587
  * @category TPaaS Tracking
558
588
  */
559
- trackViewPage(input: InputParams<IViewPage>, options: TrackViewEventOptions): Promise<unknown>;
589
+ trackViewPage(input: InputParams<IViewPage>, options: TrackViewEventOptions<IViewPage>): Promise<unknown>;
560
590
  /**
561
591
  * @param input
562
592
  * @param options
563
593
  * @category TPaaS Tracking
564
594
  */
565
- trackViewSportsPage(input: InputParams<IViewSportsPage>, options: TrackViewEventOptions): Promise<unknown>;
595
+ trackViewSportsPage(input: InputParams<IViewSportsPage>, options: TrackViewEventOptions<IViewSportsPage>): Promise<unknown>;
566
596
  /**
567
597
  * @param input
568
598
  * @category TPaaS Tracking
@@ -578,7 +608,7 @@ export default class Tracker {
578
608
  * @param options
579
609
  * @category TPaaS Tracking
580
610
  */
581
- trackViewArticle(input: InputParams<IViewArticle>, options: TrackViewEventOptions): Promise<unknown>;
611
+ trackViewArticle(input: InputParams<IViewArticle>, options: TrackViewEventOptions<IViewArticle>): Promise<unknown>;
582
612
  /**
583
613
  * Tracks a View AudioPage event and automatically fires a Leave AudioPage event when the user
584
614
  * navigates away.
@@ -619,13 +649,18 @@ export default class Tracker {
619
649
  * @param options
620
650
  * @category TPaaS Tracking
621
651
  */
622
- trackViewAudioPage(input: InputParams<IViewAudioPage>, options: TrackViewAudioPageOptions): Promise<unknown>;
652
+ trackViewAudioPage(input: InputParams<IViewAudioPage>, options: TrackViewEventOptions<IViewAudioPage>): Promise<unknown>;
623
653
  /**
624
654
  * @param input
625
655
  * @param options
626
656
  * @category TPaaS Tracking
627
657
  */
628
- trackViewError(input: InputParams<IViewError>, options: TrackViewEventOptions): Promise<unknown>;
658
+ trackViewError(input: InputParams<IViewError>, options: TrackViewEventOptions<IViewError>): Promise<unknown>;
659
+ /**
660
+ * @param input
661
+ * @category TPaaS Tracking
662
+ */
663
+ trackImpressionAdSlot(input: InputParams<IImpressionAdSlot>): Promise<unknown>;
629
664
  /**
630
665
  * @param input
631
666
  * @category TPaaS Tracking
@@ -656,37 +691,37 @@ export default class Tracker {
656
691
  * @param options
657
692
  * @category TPaaS Tracking
658
693
  */
659
- trackViewFrontpage(input: InputParams<IViewFrontpage>, options: TrackViewEventOptions): Promise<unknown>;
694
+ trackViewFrontpage(input: InputParams<IViewFrontpage>, options: TrackViewEventOptions<IViewFrontpage>): Promise<unknown>;
660
695
  /**
661
696
  * @param input
662
697
  * @param options
663
698
  * @category TPaaS Tracking
664
699
  */
665
- trackViewLandingpage(input: InputParams<IViewLandingpage>, options: TrackViewEventOptions): Promise<unknown>;
700
+ trackViewLandingpage(input: InputParams<IViewLandingpage>, options: TrackViewEventOptions<IViewLandingpage>): Promise<unknown>;
666
701
  /**
667
702
  * @param input
668
703
  * @param options
669
704
  * @category TPaaS Tracking
670
705
  */
671
- trackViewListing(input: InputParams<IViewListing>, options: TrackViewEventOptions): Promise<unknown>;
706
+ trackViewListing(input: InputParams<IViewListing>, options: TrackViewEventOptions<IViewListing>): Promise<unknown>;
672
707
  /**
673
708
  * @param input
674
709
  * @param options
675
710
  * @category TPaaS Tracking
676
711
  */
677
- trackViewLockedArticle(input: InputParams<IViewLockedArticle>, options: TrackViewEventOptions): Promise<unknown>;
712
+ trackViewLockedArticle(input: InputParams<IViewLockedArticle>, options: TrackViewEventOptions<IViewLockedArticle>): Promise<unknown>;
678
713
  /**
679
714
  * @param input
680
715
  * @param options
681
716
  * @category TPaaS Tracking
682
717
  */
683
- trackViewLockedAudioPage(input: InputParams<IViewLockedAudioPage>, options: TrackViewEventOptions): Promise<unknown>;
718
+ trackViewLockedAudioPage(input: InputParams<IViewLockedAudioPage>, options: TrackViewEventOptions<IViewLockedAudioPage>): Promise<unknown>;
684
719
  /**
685
720
  * @param input
686
721
  * @param options
687
722
  * @category TPaaS Tracking
688
723
  */
689
- trackViewLockedVideoPage(input: InputParams<IViewLockedVideoPage>, options: TrackViewEventOptions): Promise<unknown>;
724
+ trackViewLockedVideoPage(input: InputParams<IViewLockedVideoPage>, options: TrackViewEventOptions<IViewLockedVideoPage>): Promise<unknown>;
690
725
  /**
691
726
  * Tracks a View VideoPage event and automatically fires a Leave VideoPage event when the user
692
727
  * navigates away.
@@ -729,13 +764,13 @@ export default class Tracker {
729
764
  * @param options
730
765
  * @category TPaaS Tracking
731
766
  */
732
- trackViewVideoPage(input: InputParams<IViewVideoPage>, options: TrackViewVideoPageOptions): Promise<unknown>;
767
+ trackViewVideoPage(input: InputParams<IViewVideoPage>, options: TrackViewEventOptions<IViewVideoPage>): Promise<unknown>;
733
768
  /**
734
769
  * @param input
735
770
  * @param options
736
771
  * @category TPaaS Tracking
737
772
  */
738
- trackViewWeather(input: InputParams<IViewWeather>, options: TrackViewEventOptions): Promise<unknown>;
773
+ trackViewWeather(input: InputParams<IViewWeather>, options: TrackViewEventOptions<IViewWeather>): Promise<unknown>;
739
774
  /**
740
775
  * @param input
741
776
  * @category TPaaS Tracking
@@ -1244,7 +1279,7 @@ export default class Tracker {
1244
1279
  * with the added guarantee that a second Leave event will not be triggered if one already has been sent.
1245
1280
  * @category TPaaS Leave Tracking
1246
1281
  */
1247
- trackTpaasActiveLeave(): void;
1282
+ trackTpaasActiveLeave(): Promise<void>;
1248
1283
  /**
1249
1284
  * Check if leave tracking is enabled (if an event will be sent when the user leaves the page)
1250
1285
  * ```
@@ -15,7 +15,7 @@ export { subscribe, subscribeOnce, subscribeWithState, unsubscribe } from './Tra
15
15
  /**
16
16
  * TPaaS specific type exports
17
17
  */
18
- export type { AudioPageLeaveTrackingOptions, LeaveAudioInput, LeaveStoriesInput, LeaveTrackingDisabled, LeaveTrackingOptions, TrackViewAudioPageOptions, TrackViewEventOptions, TrackViewVideoPageOptions, VideoPageLeaveTrackingOptions, } from './types';
18
+ export type { LeaveAudioInput, LeaveStoriesInput, LeaveTrackingDisabled, LeaveTrackingOptions, TrackViewEventOptions, TrackViewEventOptionsForProxy, } from './types';
19
19
  /**
20
20
  * Media Wrapper Types exports below
21
21
  */
@@ -1,8 +1,3 @@
1
- export declare const DEFAULT_ACTIVITY_DURATION: () => {
2
- value: number;
3
- interval: undefined;
4
- timeSinceLastActivity: number;
5
- };
6
1
  export declare const DEFAULT_PAGE_LEAVE_EVENT: () => {
7
2
  objectViewPercentage: number;
8
3
  maxObjectViewPercentage: number;
@@ -11,3 +6,4 @@ export declare const DEFAULT_PAGE_LEAVE_EVENT: () => {
11
6
  pageScrollPosition: number;
12
7
  maxPageScrollPosition: number;
13
8
  };
9
+ export declare const ACTIVITY_DURATION_LIMIT_MS = 30000;
@@ -1,7 +1,7 @@
1
- import type { ILeave } from '@schibsted/tpaas-schemas';
2
- import type { LeaveTrackingOptions, TrackViewEventOptions } from '../types';
1
+ import type { ILeave, ITpaasEvent } from '@schibsted/tpaas-schemas';
2
+ import type { LeaveTrackingOptions, LeaveTrackingOptionsForProxy, TrackViewEventOptions } from '../types';
3
3
  import type { LeaveObjectTypes, Listener, TrackMethod } from './types';
4
- export declare function addTpaasLeaveTracking({ objectElement, pageElement, schema, objectResizable, heightBuilder, trackMethod, }: LeaveTrackingOptions & {
4
+ export declare function addTpaasLeaveTracking({ objectElement, pageElement, schema, objectResizable, heightBuilder, trackMethod, }: (LeaveTrackingOptions | LeaveTrackingOptionsForProxy) & {
5
5
  schema: LeaveObjectTypes;
6
6
  trackMethod: TrackMethod;
7
7
  }): void;
@@ -15,8 +15,8 @@ export declare function removeTpaasLeaveTracking(): void;
15
15
  */
16
16
  export declare function updateTpaasLeaveTracking(objectElement: Element, pageElement: Element, resetProperties: boolean): void;
17
17
  export declare const resetTpaasViewedContent: () => void;
18
- export declare function trackTpaasActiveLeave(): void;
19
- export declare function trackTpaasLeave(): void;
18
+ export declare function trackTpaasActiveLeave(): Promise<void>;
19
+ export declare function trackTpaasLeave(): Promise<void>;
20
20
  export declare function getTpaasLeaveObject(): ILeave;
21
21
  export declare function updateTpaasViewedContent(objectElement: Element, pageElement: Element): void;
22
22
  export declare const calculateScrolledPixels: (element: Element) => number;
@@ -25,4 +25,4 @@ export declare function addEventListeners(events: string[], handler: () => void,
25
25
  export declare function removeEventListeners(eventListeners: Listener[], options?: {
26
26
  capture?: boolean;
27
27
  }): Listener[];
28
- export declare function shouldEnableLeaveTracking(leaveOptions: TrackViewEventOptions['leaveTracking']): leaveOptions is LeaveTrackingOptions;
28
+ export declare function shouldEnableLeaveTracking(leaveOptions: TrackViewEventOptions<ITpaasEvent>['leaveTracking']): leaveOptions is LeaveTrackingOptions | LeaveTrackingOptionsForProxy;
@@ -15,14 +15,6 @@ export declare enum BrowserEvents {
15
15
  TOUCHSTART = "touchstart"
16
16
  }
17
17
  export type Listener = [name: string, listener: () => void];
18
- /**
19
- * @typedef ActivityDuration
20
- */
21
- export type ActivityDuration = {
22
- value: number;
23
- interval: number | undefined;
24
- timeSinceLastActivity: number;
25
- };
26
18
  /**
27
19
  * Used to provide extra height to view %-calculations for objects that have fake-scrolls or similar
28
20
  * For instance if you have an element that captures scroll for about 1000px
@@ -39,7 +31,7 @@ export interface AdditionalHeightInfo {
39
31
  */
40
32
  viewedHeight: number;
41
33
  }
42
- export type TrackMethod = (leave: ILeave) => void;
34
+ export type TrackMethod = (leave: ILeave) => Promise<void>;
43
35
  export type LeaveObjectTypes = Extract<ITpaasEvent, {
44
36
  eventType: 'Leave';
45
37
  }>['object']['objectType'];
@@ -17,7 +17,7 @@ export declare const MESSAGE_TYPE_INVOKE = "pulse-tracker-proxy:invoke";
17
17
  export declare const MESSAGE_TYPE_RETURN = "pulse-tracker-proxy:return";
18
18
  export declare const MESSAGE_TYPE_RETURN_ERROR = "pulse-tracker-proxy:return-error";
19
19
  export declare const ALLOWED_METHODS: readonly ["track"];
20
- export declare const TPAAS_ALLOWED_METHODS: readonly ["trackAnonymousViewUIElement", "trackEngagementAudio", "trackEngagementForm", "trackEngagementHealthUIElement", "trackEngagementOffer", "trackEngagementTeaser", "trackEngagementUIElement", "trackEngagementVideo", "trackEngagementVideoAd", "trackEngagementWidget", "trackCompletedHealthAction", "trackImpressionForm", "trackImpressionHealthUIElement", "trackImpressionOffer", "trackImpressionPlayer", "trackImpressionTeaser", "trackImpressionUIElement", "trackImpressionWidget", "trackLeaveArticle", "trackLeaveAudioPage", "trackLeaveError", "trackLeaveFrontpage", "trackLeaveLandingpage", "trackLeaveListing", "trackLeaveLockedArticle", "trackLeaveLockedAudioPage", "trackLeaveLockedVideoPage", "trackLeavePage", "trackLeaveVideoPage", "trackLeaveWeather", "trackViewArticle", "trackViewAudioPage", "trackViewError", "trackViewFrontpage", "trackViewHealthPage", "trackViewLandingpage", "trackViewListing", "trackViewLockedArticle", "trackViewLockedAudioPage", "trackViewLockedVideoPage", "trackViewPage", "trackViewTitle", "trackViewVideoPage", "trackViewWeather", "trackLegacyViewArticle", "trackLegacyViewLoginPoster", "trackLegacyViewSalesPoster", "trackLegacyViewListing", "trackLegacyViewFrontpage", "trackLegacyViewPage", "trackLegacyViewUIElement", "trackLegacyClickUIElement"];
20
+ export declare const TPAAS_ALLOWED_METHODS: readonly ["trackAnonymousViewUIElement", "trackEngagementAudio", "trackEngagementForm", "trackEngagementHealthUIElement", "trackEngagementOffer", "trackEngagementTeaser", "trackEngagementUIElement", "trackEngagementVideo", "trackEngagementVideoAd", "trackEngagementWidget", "trackCompletedHealthAction", "trackImpressionAdSlot", "trackImpressionForm", "trackImpressionHealthUIElement", "trackImpressionOffer", "trackImpressionPlayer", "trackImpressionTeaser", "trackImpressionUIElement", "trackImpressionWidget", "trackLeaveArticle", "trackLeaveAudioPage", "trackLeaveError", "trackLeaveFrontpage", "trackLeaveLandingpage", "trackLeaveListing", "trackLeaveLockedArticle", "trackLeaveLockedAudioPage", "trackLeaveLockedVideoPage", "trackLeavePage", "trackLeaveSportsPage", "trackLeaveVideoPage", "trackLeaveWeather", "trackViewArticle", "trackViewAudioPage", "trackViewError", "trackViewFrontpage", "trackViewHealthPage", "trackViewLandingpage", "trackViewListing", "trackViewLockedArticle", "trackViewLockedAudioPage", "trackViewLockedVideoPage", "trackViewPage", "trackViewSportsPage", "trackViewTitle", "trackViewVideoPage", "trackViewWeather", "trackLegacyViewArticle", "trackLegacyViewLoginPoster", "trackLegacyViewSalesPoster", "trackLegacyViewListing", "trackLegacyViewFrontpage", "trackLegacyViewPage", "trackLegacyViewUIElement", "trackLegacyClickUIElement"];
21
21
  export declare function isPulseProxyMessage(message: unknown): message is Message;
22
22
  export type TrackInvocation = {
23
23
  method: 'track';
@@ -1,4 +1,4 @@
1
- import type { ILeaveAudio, ILeaveStories } from '@schibsted/tpaas-schemas';
1
+ import type { ILeaveAudio, ILeaveStories, ITpaasEvent, IViewAudioPage, IViewVideoPage } from '@schibsted/tpaas-schemas';
2
2
  import type { v4 } from 'uuid';
3
3
  import type { Session } from './builders';
4
4
  import type { AnonymousTrackerEventInput } from './builders/events-anonymous-node';
@@ -53,41 +53,47 @@ export type LeaveAudioInput = Omit<ILeaveAudio, 'activityDurationMs' | 'duration
53
53
  * metrics when the Leave VideoPage event fires.
54
54
  */
55
55
  export type LeaveStoriesInput = Omit<ILeaveStories, 'activityDurationMs' | 'durationMs'>;
56
- export type TrackViewEventOptions = {
57
- leaveTracking: LeaveTrackingOptions | LeaveTrackingDisabled;
56
+ export type TrackViewEventOptions<T extends ITpaasEvent> = {
57
+ leaveTracking: LeaveTrackingDisabled | LeaveTrackingOptionsForProxy | (T extends IViewAudioPage ? AudioPageLeaveTrackingOptions : T extends IViewVideoPage ? VideoPageLeaveTrackingOptions : LeaveTrackingOptions);
58
+ };
59
+ export type TrackViewEventOptionsForProxy = {
60
+ leaveTracking: LeaveTrackingDisabled | LeaveTrackingOptionsForProxy;
58
61
  };
59
62
  export type LeaveTrackingOptions = {
60
- objectElement: HTMLElement;
61
- pageElement?: HTMLElement;
63
+ objectElement: Element;
64
+ pageElement?: Element;
62
65
  objectResizable?: boolean;
63
66
  heightBuilder?: () => AdditionalHeightInfo;
64
67
  };
68
+ export type LeaveTrackingOptionsForProxy = {
69
+ objectElement: string;
70
+ pageElement?: string;
71
+ objectResizable?: boolean;
72
+ heightBuilder?: never;
73
+ leaveAudioBuilder?: never;
74
+ leaveStoriesBuilder?: never;
75
+ };
65
76
  export type LeaveTrackingDisabled = false;
66
77
  /**
67
78
  * Leave tracking options for {@link Tracker.trackViewAudioPage}.
68
79
  * Extends {@link LeaveTrackingOptions} with an optional builder for audio engagement statistics.
69
80
  */
70
- export type AudioPageLeaveTrackingOptions = LeaveTrackingOptions & {
81
+ type AudioPageLeaveTrackingOptions = LeaveTrackingOptions & {
71
82
  /**
72
83
  * Called at leave time to provide audio engagement statistics for the Leave AudioPage event.
73
84
  * If not provided, all audio engagement fields default to `0`.
74
85
  */
75
86
  leaveAudioBuilder?: () => LeaveAudioInput;
76
87
  };
77
- export type TrackViewAudioPageOptions = {
78
- leaveTracking: AudioPageLeaveTrackingOptions | LeaveTrackingDisabled;
79
- };
80
88
  /**
81
89
  * Leave tracking options for {@link Tracker.trackViewVideoPage}.
82
90
  * Extends {@link LeaveTrackingOptions} with an optional builder for story carousel statistics.
83
91
  */
84
- export type VideoPageLeaveTrackingOptions = LeaveTrackingOptions & {
92
+ type VideoPageLeaveTrackingOptions = LeaveTrackingOptions & {
85
93
  /**
86
94
  * Called at leave time to provide story carousel statistics for the Leave VideoPage event.
87
95
  * If not provided, `leaveStories` will be omitted from the Leave VideoPage event.
88
96
  */
89
97
  leaveStoriesBuilder?: () => LeaveStoriesInput;
90
98
  };
91
- export type TrackViewVideoPageOptions = {
92
- leaveTracking: VideoPageLeaveTrackingOptions | LeaveTrackingDisabled;
93
- };
99
+ export {};
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "8.0.0-rc.5";
1
+ export declare const SDK_VERSION = "8.0.0-rc.7";
@@ -1,4 +1,7 @@
1
1
  type ComponentViewCallback = (node: HTMLElement) => void;
2
+ export interface ObserverOptions {
3
+ threshold?: number;
4
+ }
2
5
  /**
3
6
  * Observes visibility of elements matching a selector using MutationObserver and IntersectionObserver.
4
7
  */
@@ -16,9 +19,11 @@ export declare class VisibilityObserver {
16
19
  * Uses {@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | MutationObserver} to monitor the DOM for dynamically loaded content.
17
20
  * Callback is invoked only once per element.
18
21
  * @param selector The CSS selector for elements to track.
22
+ * @param options {ObserverOptions} Optional configuration for the observer.
23
+ * @param options.threshold Which ratio triggers the Observer callback.
19
24
  * @throws Error if called more than once.
20
25
  */
21
- observe(selector: string): void;
26
+ observe(selector: string, { threshold }: ObserverOptions): void;
22
27
  /**
23
28
  * @returns {HTMLElement[]} An array of elements that match the selector in the document.
24
29
  */
@@ -1,5 +1,7 @@
1
1
  type NavigationType = 'navigation' | 'alternative' | 'refresh' | 'unknown';
2
- type PageType = 'Article' | 'Listing' | 'Frontpage' | 'SalesPoster' | 'Page' | 'LoginPoster';
2
+ declare const LEGACY_PAGE_TYPE_VALUES: readonly ["Article", "Listing", "Frontpage", "SalesPoster", "Page", "LoginPoster"];
3
+ export declare function isLegacyPageType(value: string): value is PageType;
4
+ type PageType = (typeof LEGACY_PAGE_TYPE_VALUES)[number];
3
5
  type TargetCategory = 'Article' | 'Audio' | 'Video' | 'Weather' | 'Title' | 'other';
4
6
  type TargetType = 'Article' | 'ArticleReference' | 'ExternalContent';
5
7
  type SalesPosterType = 'Ip' | 'Login' | 'Paid' | 'Meter' | 'Personalised';