@schibsted/niche-tracking
Advanced tools
Comparing version 4.5.0 to 4.5.1
@@ -37,2 +37,3 @@ export default Tracker; | ||
lastPageViewEventData: {}; | ||
eventTarget: EventTarget; | ||
/** | ||
@@ -55,2 +56,3 @@ * @param type string of ROUTE_PATTERNS | ||
trackActivePageLeave(type: any, eventName: any): void; | ||
waitForInitialization: () => Promise<any>; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { getEventTarget } from '@schibsted/niche-utils/lib/eventTarget'; | ||
import { isWeb, isPromise } from './utils'; | ||
@@ -33,2 +34,12 @@ /** | ||
constructor({ logger = defaultLogger, isLoggingEnabled, enabled, trackers, pageviewTrackerMappers, eventTrackerMappers, mutateScript, pageLeaveTrackerMappers, }) { | ||
this.waitForInitialization = () => new Promise((resolve) => { | ||
if (this.initialized) { | ||
resolve(); | ||
} | ||
else { | ||
this.eventTarget.addEventListener('initialized', () => { | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
this.logger = isLoggingEnabled ? logger : defaultLogger; | ||
@@ -45,2 +56,3 @@ this.trackers = trackers; | ||
this.lastPageViewEventData = {}; | ||
this.eventTarget = getEventTarget(); | ||
} | ||
@@ -173,2 +185,3 @@ /** | ||
this.initialized = true; | ||
this.eventTarget.dispatchEvent(new Event('initialized')); | ||
while (this.queue.length) { | ||
@@ -175,0 +188,0 @@ const queuedJob = this.queue.shift(); |
{ | ||
"name": "@schibsted/niche-tracking", | ||
"version": "4.5.0", | ||
"version": "4.5.1", | ||
"description": "Package containing tracking logic reused between multiple Schibsted niche sites", | ||
@@ -22,3 +22,6 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "fc08919b5ab8137d2c8f835848aca018c563731b" | ||
"dependencies": { | ||
"@schibsted/niche-utils": "^0.1.1" | ||
}, | ||
"gitHead": "bfedafe9f39182f0b90b6df9a1e7fd0eb6f3c070" | ||
} |
103
README.md
# Niche Tracking | ||
## Description | ||
This repository contains code we use for tracking. The goal of niche-tracking is to be used in all of our products. Currently it's used by [tek-web](https://github.schibsted.io/niche-and-services/tek-web), [godt-web](https://github.schibsted.io/niche-and-services/godt2-web) and [pent-web](https://github.schibsted.io/niche-and-services/pent2-web). | ||
## Local development | ||
Nothing fancy here. First install dependencies | ||
@@ -21,2 +23,3 @@ clone repository, open terminal and run. | ||
### Linking | ||
Note: order of command execution is important. | ||
@@ -39,9 +42,13 @@ | ||
### Unlinking | ||
After you end working on the package you need to unlink it. | ||
First, in the project where the package was used: | ||
```bash | ||
npm unlink --no-save @schibsted/niche-tracking | ||
``` | ||
Second, in the niche-tracking package: | ||
```bash | ||
@@ -52,5 +59,7 @@ npm unlink | ||
## Supported trackers | ||
Currently pulse, google analytics and mbl trackers are implemented. | ||
## How to use it and how it works | ||
Package exports `Tracker` class and all implemented tracker objects. You can import it like this: | ||
@@ -80,26 +89,32 @@ | ||
`tracker.initialize` methods takes config with tracker specific options. You can see them here: | ||
- [pulse](./src/pulse/README.md) | ||
- [google analytics](./src/googleAnalytics/README.md) | ||
- [mbl](./src/mbl/README.md) | ||
- [pulse](./src/pulse/README.md) | ||
- [google analytics](./src/googleAnalytics/README.md) | ||
- [mbl](./src/mbl/README.md) | ||
Let's discuss Tracker constructor parameters | ||
### logger | ||
Optional object containing 4 methods: | ||
- log | ||
- info | ||
- warn | ||
- error | ||
- log | ||
- info | ||
- warn | ||
- error | ||
### trackers | ||
Array with trackers you want to use. | ||
You can import those from package or provide your own. They must be objects with three methods: | ||
- pageview | ||
- event | ||
- initialize | ||
- [navigationEvent](#navigationevent) (optional) | ||
- addPageLeaveTracking | ||
- trackActivePageLeave | ||
- pageview | ||
- event | ||
- initialize | ||
- [navigationEvent](#navigationevent) (optional) | ||
- addPageLeaveTracking | ||
- trackActivePageLeave | ||
### pageViewTrackerMappers, eventTrackerMappers, pageLeaveTrackerMappers | ||
These are objects with following format: | ||
@@ -116,2 +131,3 @@ | ||
### enabled | ||
This flag lets you disable the tracking. Please keep in mind that even if you set `enabled: false` mappers will still be called to keep the behaviour as close to production as it can be. | ||
@@ -126,3 +142,3 @@ | ||
[SCREENS.SECTION]: { | ||
pulse: options => ({ | ||
pulse: (options) => ({ | ||
object: { | ||
@@ -138,14 +154,15 @@ name: window.document.title, | ||
}), | ||
googleAnalytics: options => ({ | ||
googleAnalytics: (options) => ({ | ||
page_path: options.location.pathname, | ||
}), | ||
mbl: options => ({ | ||
mbl: (options) => ({ | ||
url: window.location.toString(), | ||
title: window.document.title, | ||
image: options.image.url, | ||
referrer: document.referrer | ||
}) | ||
} | ||
} | ||
referrer: document.referrer, | ||
}), | ||
}, | ||
}; | ||
``` | ||
Remember out tracker instance? Just to remind you: | ||
@@ -166,6 +183,7 @@ | ||
Now each time you call `tracker.pageView(SCREENS.SECTION, options)` the tracker will create: | ||
- pulseSpecificOptions, | ||
- googleAnalyticsSpecificOptions | ||
- mblSpecificOptions | ||
- pulseSpecificOptions, | ||
- googleAnalyticsSpecificOptions | ||
- mblSpecificOptions | ||
according to transform functions you provided in `pageViewTrackerMappers`. | ||
@@ -184,5 +202,5 @@ | ||
```js | ||
mutateScript: script => { | ||
mutateScript: (script) => { | ||
script.classList.add('no-prerender'); | ||
} | ||
}; | ||
``` | ||
@@ -194,2 +212,3 @@ | ||
For example it could look like this: | ||
```javascript | ||
@@ -208,3 +227,3 @@ const tracker = new Tracker({ | ||
name: 'Readable explanation of A/B test', | ||
variant: 'variant-b' // identifier of a variant which will be served to user | ||
variant: 'variant-b', // identifier of a variant which will be served to user | ||
}, | ||
@@ -214,3 +233,3 @@ { | ||
name: 'Readable explanation of another A/B test', | ||
variant: 'another-variant-a' | ||
variant: 'another-variant-a', | ||
}, | ||
@@ -222,4 +241,5 @@ ]; | ||
experiments, | ||
}) | ||
}); | ||
``` | ||
In the case of the `pulse` tracker, this list will be added to every page view and engagement event. | ||
@@ -230,12 +250,15 @@ | ||
**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. | ||
- 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 | ||
@@ -249,2 +272,3 @@ | ||
To make the release work you need to do the following: | ||
1. Generate a Github personal token with `repo` credentials. | ||
@@ -255,13 +279,16 @@ 2. Set the token as `GITHUB_TOKEN` environment variable. | ||
Release-it does the following: | ||
- makes sure you have your workspace clean and `GITHUB_TOKEN` set | ||
- bumps the version in `package.json` and `package-lock.json` | ||
- commits the changes | ||
- tags the commit with the version number | ||
- pushes the changes and tag | ||
- creates a Github Release with the changelog since the last release | ||
- makes sure you have your workspace clean and `GITHUB_TOKEN` set | ||
- bumps the version in `package.json` and `package-lock.json` | ||
- commits the changes | ||
- tags the commit with the version number | ||
- pushes the changes and tag | ||
- creates a Github Release with the changelog since the last release | ||
## Polyfills | ||
This package requires only one polyfill: Promise. | ||
## Useful links | ||
To get more general idea of how tracking works check our [team docs](https://pages.github.schibsted.io/niche-and-services/team/tracking) |
# Google analytics tracker | ||
## Config | ||
This tracker needs additional config: | ||
- `cdn` (optional, `https://www.googletagmanager.com/gtag/js?id=` by default) | ||
- `id` (required) | ||
- `cdn` (optional, `https://www.googletagmanager.com/gtag/js?id=` by default) | ||
- `id` (required) | ||
Google Analytics config can be passed to initialize function | ||
Google Analytics config can be passed to initialize function | ||
```javascript | ||
@@ -19,5 +21,5 @@ const tracker = new Tracker({ | ||
options: { | ||
'anonymize_ip': true, | ||
'country': 'PL', | ||
} | ||
anonymize_ip: true, | ||
country: 'PL', | ||
}, | ||
}, | ||
@@ -29,2 +31,2 @@ }); | ||
IP anonymization is on by default. | ||
IP anonymization is on by default. |
@@ -6,4 +6,5 @@ # MBL tracker | ||
## Config | ||
This tracker needs no additional config: | ||
- `cdn` (optional, `https://log.medietall.no/analytics.js` by default) | ||
- `cdn` (optional, `https://log.medietall.no/analytics.js` by default) |
# Pulse tracker | ||
## Config | ||
This tracker needs additional config: | ||
- `cdn` (optional, `https://sdk.pulse.schibsted.com/loader-modern.min.js` by default) | ||
- `sdkConfigInput` [see](https://github.schibsted.io/spt-dataanalytics/pulse-sdk-js#sdkconfiginput) | ||
- `clientId` (required) simple string like `tek`, `godt`, `tek-stage` etc | ||
- `builders` [see](https://github.schibsted.io/spt-dataanalytics/pulse-sdk-js#builders) | ||
- `cdn` (optional, `https://sdk.pulse.schibsted.com/loader-modern.min.js` by default) | ||
- `sdkConfigInput` [see](https://github.schibsted.io/spt-dataanalytics/pulse-sdk-js#sdkconfiginput) | ||
- `clientId` (required) simple string like `tek`, `godt`, `tek-stage` etc | ||
- `builders` [see](https://github.schibsted.io/spt-dataanalytics/pulse-sdk-js#builders) |
@@ -0,1 +1,2 @@ | ||
import { getEventTarget } from '@schibsted/niche-utils/lib/eventTarget'; | ||
import { isWeb, isPromise } from './utils'; | ||
@@ -58,2 +59,3 @@ | ||
this.lastPageViewEventData = {}; | ||
this.eventTarget = getEventTarget(); | ||
} | ||
@@ -198,2 +200,3 @@ | ||
this.initialized = true; | ||
this.eventTarget.dispatchEvent(new Event('initialized')); | ||
@@ -297,4 +300,15 @@ while (this.queue.length) { | ||
} | ||
waitForInitialization = () => | ||
new Promise((resolve) => { | ||
if (this.initialized) { | ||
resolve(); | ||
} else { | ||
this.eventTarget.addEventListener('initialized', () => { | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
} | ||
export default Tracker; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
61377
1325
279
0
1
+ Added@schibsted/niche-utils@0.1.2(transitive)