@schibsted/niche-tracking
Advanced tools
Comparing version 4.2.1 to 4.3.0
@@ -28,2 +28,3 @@ "use strict"; | ||
let experiments = []; | ||
let navigationEventObject; | ||
@@ -80,2 +81,10 @@ const setUserId = (id, realm) => { | ||
const pageview = async options => { | ||
let delayedEvent; // Take the value of the `delayedEventObject` variable before doing any async work | ||
// so that this value should be taken by the first page view invocation. | ||
if (navigationEventObject) { | ||
delayedEvent = navigationEventObject; | ||
navigationEventObject = null; | ||
} | ||
_state.default.updateUrl(options); | ||
@@ -96,2 +105,8 @@ | ||
if (delayedEvent) { | ||
await basicInstance.track(delayedEvent.type, _objectSpread(_objectSpread({}, delayedEvent.event), {}, { | ||
target: extendedObject | ||
})); | ||
} | ||
await basicInstance.trackPageView(_objectSpread(_objectSpread(_objectSpread({}, options), getExperiments()), {}, { | ||
@@ -121,2 +136,22 @@ object: extendedObject, | ||
const navigationEvent = async (options, trackerOptions) => { | ||
const origin = _state.default.getOrigin(); | ||
const eventInputs = await basicInstance.evaluateEventInputs(); | ||
navigationEventObject = { | ||
type: trackerOptions?.eventType ?? 'engagementEvent', | ||
event: _objectSpread(_objectSpread(_objectSpread({}, options), getExperiments()), {}, { | ||
object: _objectSpread(_objectSpread({}, options.object), {}, { | ||
custom: _objectSpread(_objectSpread({}, options.object.custom), {}, { | ||
'spt:device': (0, _utils.get)('device.deviceType', eventInputs), | ||
'spt:url': (0, _utils.get)('object.url', eventInputs), | ||
'spt:permalink': (0, _utils.get)('object.url', eventInputs) | ||
}) | ||
}), | ||
origin: (0, _helpers.originToEngagementFormat)(origin) | ||
}) | ||
}; | ||
logger.log('[Pulse] Navigation Event:', options); | ||
}; | ||
const getInstance = () => basicInstance; | ||
@@ -126,2 +161,3 @@ | ||
event: (0, _helpers.withReferrer)(event), | ||
navigationEvent: (0, _helpers.withReferrer)(navigationEvent), | ||
pageview: (0, _helpers.withReferrer)(pageview), | ||
@@ -128,0 +164,0 @@ initialize, |
@@ -29,2 +29,3 @@ "use strict"; | ||
* @property {function} event | ||
* @property {function} navigationEvent | ||
*/ | ||
@@ -158,3 +159,45 @@ | ||
} | ||
/** Creates event but waits with sending it until next pageView invocation. */ | ||
navigationEvent(type) { | ||
for (var _len3 = arguments.length, options = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
options[_key3 - 1] = arguments[_key3]; | ||
} | ||
const { | ||
trackers, | ||
eventTrackerMappers, | ||
enabled, | ||
initialized | ||
} = this; | ||
trackers.filter(tracker => tracker.navigationEvent).filter(tracker => eventTrackerMappers?.[type]?.[tracker.name]).forEach(tracker => { | ||
let trackerOptions; | ||
let skip = false; | ||
try { | ||
const getTrackerOptions = eventTrackerMappers?.[type]?.[tracker.name]; | ||
trackerOptions = getTrackerOptions(...options, this.lastPageViewEventData?.[tracker.name]); | ||
} catch (err) { | ||
this.logger.error(`[${tracker.name}] Event mapper for ${type} failed`, err); | ||
skip = true; | ||
} | ||
if (enabled && !skip) { | ||
if (initialized) { | ||
try { | ||
if (Array.isArray(trackerOptions) && trackerOptions.length === 2) { | ||
tracker.navigationEvent(...trackerOptions); | ||
} else { | ||
tracker.navigationEvent(trackerOptions); | ||
} | ||
} catch (e) {// ignore | ||
} | ||
} else { | ||
this.queue.push(() => tracker.navigationEvent(trackerOptions)); | ||
} | ||
} | ||
}); | ||
} | ||
initialize(config) { | ||
@@ -161,0 +204,0 @@ if (!(0, _utils.isWeb)()) { |
{ | ||
"name": "@schibsted/niche-tracking", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"description": "Package containing tracking logic reused between multiple Schibsted niche sites", | ||
@@ -48,3 +48,3 @@ "main": "dist/index.js", | ||
"@release-it/conventional-changelog": "^4.3.0", | ||
"eslint": "^8.19.0", | ||
"eslint": "^8.20.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
@@ -51,0 +51,0 @@ "eslint-config-prettier": "^8.5.0", |
@@ -95,2 +95,3 @@ # Niche Tracking | ||
- initialize | ||
- [navigationEvent](#navigationevent) (optional) | ||
@@ -210,2 +211,15 @@ ### pageViewTrackerMappers, eventTrackerMappers | ||
## NavigationEvent | ||
**NOTE:** | ||
> Use this method for **internal navigation** only. | ||
> | ||
> It can fire only **ONE** (latest) `navigationEvent` per `pageView` invocation. | ||
What this method does is: | ||
- it creates an event like it would normally do, | ||
- **postpones sending this event** until next `pageView` invocation, | ||
- once `pageView` is invoked the pending event will have `tracker` property value set to whatever the `object` property of current `pageView` event is. | ||
In short: it will remove necessity of manual creation of `target` property of an event in case of internal navigation. | ||
## Deployment | ||
@@ -212,0 +226,0 @@ |
@@ -10,2 +10,3 @@ import { loadScript, get } from '../utils'; | ||
let experiments = []; | ||
let navigationEventObject; | ||
const setUserId = (id, realm) => { | ||
@@ -55,2 +56,10 @@ logger.log('[Pulse] Setting user id:', id); | ||
const pageview = async (options) => { | ||
let delayedEvent; | ||
// Take the value of the `delayedEventObject` variable before doing any async work | ||
// so that this value should be taken by the first page view invocation. | ||
if (navigationEventObject) { | ||
delayedEvent = navigationEventObject; | ||
navigationEventObject = null; | ||
} | ||
state.updateUrl(options); | ||
@@ -71,2 +80,9 @@ const eventInputs = await basicInstance.evaluateEventInputs(); | ||
if (delayedEvent) { | ||
await basicInstance.track(delayedEvent.type, { | ||
...delayedEvent.event, | ||
target: extendedObject, | ||
}); | ||
} | ||
await basicInstance.trackPageView({ | ||
@@ -104,2 +120,28 @@ ...options, | ||
const navigationEvent = async (options, trackerOptions) => { | ||
const origin = state.getOrigin(); | ||
const eventInputs = await basicInstance.evaluateEventInputs(); | ||
navigationEventObject = { | ||
type: trackerOptions?.eventType ?? 'engagementEvent', | ||
event: { | ||
...options, | ||
...getExperiments(), | ||
object: { | ||
...options.object, | ||
custom: { | ||
...options.object.custom, | ||
'spt:device': get('device.deviceType', eventInputs), | ||
'spt:url': get('object.url', eventInputs), | ||
'spt:permalink': get('object.url', eventInputs), | ||
}, | ||
}, | ||
origin: originToEngagementFormat(origin), | ||
}, | ||
}; | ||
logger.log('[Pulse] Navigation Event:', options); | ||
}; | ||
const getInstance = () => basicInstance; | ||
@@ -109,2 +151,3 @@ | ||
event: withReferrer(event), | ||
navigationEvent: withReferrer(navigationEvent), | ||
pageview: withReferrer(pageview), | ||
@@ -111,0 +154,0 @@ initialize, |
@@ -16,2 +16,3 @@ import { isWeb, isPromise } from './utils'; | ||
* @property {function} event | ||
* @property {function} navigationEvent | ||
*/ | ||
@@ -131,2 +132,38 @@ | ||
/** Creates event but waits with sending it until next pageView invocation. */ | ||
navigationEvent(type, ...options) { | ||
const { trackers, eventTrackerMappers, enabled, initialized } = this; | ||
trackers | ||
.filter((tracker) => tracker.navigationEvent) | ||
.filter((tracker) => eventTrackerMappers?.[type]?.[tracker.name]) | ||
.forEach((tracker) => { | ||
let trackerOptions; | ||
let skip = false; | ||
try { | ||
const getTrackerOptions = eventTrackerMappers?.[type]?.[tracker.name]; | ||
trackerOptions = getTrackerOptions(...options, this.lastPageViewEventData?.[tracker.name]); | ||
} catch (err) { | ||
this.logger.error(`[${tracker.name}] Event mapper for ${type} failed`, err); | ||
skip = true; | ||
} | ||
if (enabled && !skip) { | ||
if (initialized) { | ||
try { | ||
if (Array.isArray(trackerOptions) && trackerOptions.length === 2) { | ||
tracker.navigationEvent(...trackerOptions); | ||
} else { | ||
tracker.navigationEvent(trackerOptions); | ||
} | ||
} catch (e) { | ||
// ignore | ||
} | ||
} else { | ||
this.queue.push(() => tracker.navigationEvent(trackerOptions)); | ||
} | ||
} | ||
}); | ||
} | ||
initialize(config) { | ||
@@ -133,0 +170,0 @@ if (!isWeb()) { |
55421
1183
248