# @bbc/newslabs-helper-analytics

> A simple helper to simplify pulling analytics into our projects without having to write boilerplate everytime

Latest version **3.5.0** (published 2020-10-08) · BBC license · 0 weekly downloads

## Install

```sh
npm install @bbc/newslabs-helper-analytics
pnpm add @bbc/newslabs-helper-analytics
yarn add @bbc/newslabs-helper-analytics
bun add @bbc/newslabs-helper-analytics
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.5.0 |
| Published | 2020-10-08 |
| First published | 2019-03-15 |
| Weekly downloads | 0 |
| License | BBC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 185.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | newslabs-development@lists.forge.bbc.co.uk |
| Maintainers | tamsingreen, roohutton, joewhitwell, cmolumby, allishultes, oduyer02, leihe, jamesdools, manishlad_bbc, emettely, langtonm |
| Keywords | bbc, newslabs, analytics, helper |

## Links

- npm: https://www.npmjs.com/package/@bbc/newslabs-helper-analytics
- Repository: https://github.com/bbc/newslabs-helper-analytics
- Homepage: https://github.com/bbc/newslabs-helper-analytics#readme
- Issues: https://github.com/bbc/newslabs-helper-analytics/issues
- npm.io page: https://npm.io/package/@bbc/newslabs-helper-analytics

## Dependencies (2)

- [es6-promise](https://npm.io/package/es6-promise.md) ^4.2.6
- [@bbc/echo-client-js](https://npm.io/package/@bbc/echo-client-js.md) 12.12.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.5.0 (latest) — 2020-10-08
- 3.1.0 (beta) — 2019-08-01
- 3.4.0 — 2020-01-17
- 3.3.0 — 2020-01-17
- 3.2.0 — 2020-01-10
- 3.1.3 — 2019-08-01
- 3.1.2 — 2019-08-01
- 3.1.1 — 2019-08-01
- 3.0.0 — 2019-07-19
- 2.0.0 — 2019-05-10
- 1.1.0 — 2019-04-30
- 1.0.0 — 2019-03-15

## README

# @bbc/newslabs-helper-analytics

A simple helper to simplify pulling analytics into our projects without having to write boilerplate everytime

It is implemented using ES5 to be compatible with IE11 that has yet to adopt ES6

It is implemented in a function style

It mainly acts as a wrapper for Echo that makes it easier to calibrate:<br>
[_Echo Docs_](http://bbc.github.io/echo-docs/_)

---
## Setup
```
npm i @bbc/newslabs-helper-analytics
```

---
## Usage
Importing this package provies a single function that accepts an options object to calibrate Echo as needed for any given project

The helper must be instantiated with a string designating the name of your app or project

```javascript
import analytics from '@bbc/newslabs-helper-analytics';

const options = {
  destination: 'NEWS_PS',
  labels: {
      name: 'newslabs.example.page',
      ml_name: 'echo',
      ml_version: '11.0.1',
      bbc_site: 'aboutthebbc',
  },
}

const analyticsHelper = analytics('example project name', options);

analyticsHelper.viewEvent('page-name', { actionName: 'page-load' });
analyticsHelper.userActionEvent('CLICKED_HERE', { campaignId: 'page-name' });
```

---
## Echo Options Object
The Echo options object follows the following schema

[Echo Docs: Reporting to ATI](http://bbc.github.io/echo-docs/documentation/reporting-ati/)


| Key         | Type    | Optional? | Defaults | Value  |
| ----------- | ------- | --------- | -------- | ------ |
| destination | string  | optional | NEWS_PS | The ATI destination for your stats<br/>[see Echo Desination docs](http://bbc.github.io/echo-docs/documentation/reporting-ati/#producer)
| labels      | object  | optional | none | key-value pairs to be fed to Echo's ```.addLabel()``` function
| appVersion  | string  | optional | none | Tells echo the version of your app
| events      | Array[string] | optional | none | produces custom event methods that can be used to measure seen/click interaction ratios
---
## Default Methods

These are the methods that are supported natively by Echo

### ```.viewEvent(countername, eventLabels)```

Used to fire a page load stat

This should be called once, at the point where the page is known to have finished loading

[Echo Docs: Section](http://bbc.github.io/echo-docs/documentation/reporting-ati/#section)<br>
[Echo Docs: Site Variables](http://bbc.github.io/echo-docs/documentation/reporting-ati/#site-variables)

| Arguments | Type  | Purpose
| --------- | ----- | -------
| Countername | string | Name of the page
| eventLabels | object | see below

### ```.userActionEvent(actionName, eventLabels)```

Used to fire a user action stat

These can be called anywhere that a user interaction with the page needs to be monitored

[Echo Docs: Valid User Actions](http://bbc.github.io/echo-docs/documentation/reporting-ati/#user-action-events)

| Arguments | Type  | Purpose
| --------- | ----  | -------
| actionName  | string | Name of the action
| eventLabels | object | see below

## Custom Event Methods

Used to track specific events in an Seen/Clicked ratio manner<br>

By adding an `events` key to the options object you can supply and array of custom events that you would like monitor using ATI's impression to click ratio<br>

### ```.event(type, eventLabels)```

| Arguments   | Type   | Purpose
| ---------   | ----   | -------
| type        | 0 or 1 | 0 designates an impression, 1 designates a click
| eventLabels | object | see below

The implementation of this works around some strange behaviour in Echo wherein the `is_background` eventLabel does not respond to being true or false.
Instead, the presence of the key (regardless of the value) is taken to indicate the userActionEvent is an impression - and when it is missing the userActionEvent is counted as a click

## Event Labels

| Key | Type | Default Value | Field in ATI dashboard | Purpose |
| - | - | - | - | - |
| container | string | 'page' | campaignId | Name the current page
| metadata | string | current page name | format | Pass through additional infomation
| actionType | string | '' | concatinates to the end of the Action name | Designate type of the action
| personalisation | string | none | variant | Designate an experiment variant
| source | string | none | advertiserId | designate the source id of the event
| result | string | none | url | The url that a click event leads through to

---
## Viewing in ATI

By default, your stats will show up in ATI under the PS_NEWS namespace.

Page View Event counts can be seen under `CONTENT` -> `Pages`

User Action Events and custom Impression Click events ratios can be viewed under `CONTENT` -> `On-site ads`

![](./docs/ati.png)

---
## System Architecture

The package uses [RequireJS](https://requirejs.org/) to load Echo

RequireJS must be added to the window using a script tag, but this will be handled on any BBC page that already loads the Orbit header

```HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
```

The package then uses a functional approach to resolve the asynchronous loading of Echo without this needing to be worried about by your project

It aims to provide the same key methods that Echo uses to fire stats that we need to use

---
## Development env
Use [npm link](https://docs.npmjs.com/cli/link.html) to link a local version of this package to a project that uses this package

```
npm link @bbc/newslabs-helper-analytics
```
---
## Tests

_uses Jest_

Running the tests also provides a coverage report

```
npm test
```

---
## Release

Releases to npm should use [symantic versioning](https://semver.org/)

For any release, a release note outlining the changes and linking to the PR should be published through Github releases

```
npm version
npm publish
```

## Contact
Please contact [BBC News Labs Team](BBCNewsLabsTeam@bbc.co.uk) if you want to get in touch

---
_Source: https://npm.io/package/@bbc/newslabs-helper-analytics · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
