npm package: @newrelic/video-core
Description: New Relic video tracking core library
Author: Jordi Aguilar
Latest version: 4.1.5

[![Community Project header](https://github.com/newrelic/open-source-office/raw/master/examples/categories/images/Community_Project.png)](https://github.com/newrelic/open-source-office/blob/master/examples/categories/index.md#community-project)

# New Relic Video Core - JavaScript

The New Relic video tracking core library is the base for all video trackers in the browser platform. It contains the classes and core mechanisms used by the player specific trackers.
It segregates the events into different event types based on action, such as video-related events going to `VideoAction`, ad-related events to `VideoAdAction`, errors to `VideoErrorAction`, and custom actions to `VideoCustomAction`.

## Registering Trackers

Any browser-based video tracker can extend the `VideoTracker` class and use its core functionality.

To initialize a tracker, create an instance of your specific tracker class:

```javascript
const options = {
  info: {
    licenseKey: "xxxxxxxxxxx",
    beacon: "xxxxxxxxxx",
    applicationId: "xxxxxxx",
  },
  config: {
    qoeAggregate: false, // Optional: Enable/disable QoE (Quality of Experience) event aggregation (default: false)
    qoeIntervalFactor: 2, // Optional: Include QoE aggregate events once every N harvest cycles; must be a whole number (e.g. 2, not 2.1) (default: 1)
  },
};

// User can get the `info` object by completing the onboarding process on New Relic.
const tracker = new VideoSpecificTracker(player, options);
```

### Configuration Options

- **qoeAggregate** (boolean, optional, default: `false`): Controls whether Quality of Experience (QoE) events are aggregated and sent to New Relic. Set to `true` if you want to enable QoE event collection.

- **qoeIntervalFactor** (number, optional, default: `1`): Controls how frequently QoE aggregate events are included in harvest cycles. A value of `1` includes them on every cycle; a value of `N` includes them once every N cycles. Must be a positive integer — invalid values default to `1`. QoE events are always included on the first and final harvest cycles regardless of this setting.

- **obfuscate** (array, optional): A list of regex-based obfuscation rules applied to event payloads before they are sent to the New Relic collector. See [Obfuscation Rules](#obfuscation-rules) below.


## Obfuscation Rules

Video trackers can capture rich telemetry (content URLs, titles, ad metadata, etc.) that may inadvertently contain PII or sensitive tokens. The `obfuscate` config option lets you define regex-based rules that mask sensitive data in the serialized event payload **before** it is sent to the New Relic collector.

This matches the approach used by the New Relic Browser Agent.

### Configuration

Pass an `obfuscate` array in the `config` parameter of `setVideoConfig` (or the tracker options). Each rule must have:

| Field | Type | Description |
|-------|------|-------------|
| `regex` | `string` \| `RegExp` | Pattern to match in the serialized payload |
| `replacement` | `string` | String to replace each match with |

```javascript
const options = {
  info: {
    licenseKey: "xxxxxxxxxxx",
    beacon: "xxxxxxxxxx",
    applicationId: "xxxxxxx",
  },
  config: {
    obfuscate: [
      { regex: /\/user\/\d+/, replacement: "/user/[REDACTED]" },
    ],
  },
};

const tracker = new VideoSpecificTracker(player, options);
```

### Examples

**Mask user IDs in content URLs:**

```javascript
obfuscate: [
  { regex: /\/user\/[^\/?"]+/, replacement: "/user/[REDACTED]" }
]
// "https://cdn.example.com/user/john.doe/video.mp4"
// → "https://cdn.example.com/user/[REDACTED]/video.mp4"
```

**Mask auth tokens in query strings:**

```javascript
obfuscate: [
  { regex: /token=[^&"]+/, replacement: "token=[MASKED]" }
]
// "https://cdn.example.com/stream.m3u8?token=eyJhbGci..."
// → "https://cdn.example.com/stream.m3u8?token=[MASKED]"
```

### Rule ordering

Rules are applied in array order. The output of one rule is the input for the next, so ordering matters when rules overlap.

### Edge cases

- **Invalid regex**: The rule is skipped and a warning is logged via `Log.warn`. Other rules continue to apply normally.
- **Empty replacement**: Setting `replacement: ""` deletes the matched content entirely.
- **RegExp flags**: The global flag (`g`) is always applied so all occurrences in the payload are replaced. Other flags (`i`, `m`, etc.) on `RegExp` objects are preserved.


## APIs

Some of the APIs exposed and commonly used are:

- `tracker.setUserId("userId")` &mdash; Set the user ID.
- `tracker.setHarvestInterval(30000)` &mdash; Set the harvest interval time (in milliseconds).
- `tracker.setOptions({ customData: { key: value } })` &mdash; Set custom options or data.
- `tracker.sendCustom("CustomEvent", { data: "custom-test" })` &mdash; Send a custom event.

Any event emitted by the tracker will be sent to New Relic and processed according to its type.

Once the tracker is added, any event it emits will be sent to New Relic and processed by the following functions:

```javascript
tracker.sendVideoAction("VideoEvent", { data: 1 });
tracker.sendVideoAdAction("AdEvent", { data: "test-1" });
tracker.sendVideoErrorAction("ErrorEvent", { data: "error-test" });
tracker.sendVideoCustomAction("CustomEvent", { data: "custom-test" });
```

## Data Model

To understand which actions and attributes are captured and emitted by the tracker under different event types, see [DataModel.md](DATAMODEL.md).

## Documentation

All classes are documented using autodocs. The documents, generated with [jsdoc](https://github.com/jsdoc/jsdoc), can be found in the `documentation` directory of the current repo.

# Support

New Relic has open-sourced this project. This project is provided AS-IS WITHOUT WARRANTY OR DEDICATED SUPPORT. Issues and contributions should be reported to the project here on GitHub.

We encourage you to bring your experiences and questions to the [Explorers Hub](https://discuss.newrelic.com) where our community members collaborate on solutions and new ideas.

## Community

New Relic hosts and moderates an online forum where customers can interact with New Relic employees as well as other customers to get help and share best practices. Like all official New Relic open source projects, there's a related Community topic in the New Relic Explorers Hub. You can find this project's topic/threads here:

https://discuss.newrelic.com/t/video-core-js-tracker/100303

## Issues / enhancement requests

Issues and enhancement requests can be submitted in the [Issues tab of this repository](../../issues). Please search for and review the existing open issues before submitting a new issue.

# Contributing

Contributions are encouraged! If you submit an enhancement request, we'll invite you to contribute the change yourself. Please review our [Contributors Guide](CONTRIBUTING.md).

Keep in mind that when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. If you'd like to execute our corporate CLA, or if you have any questions, please drop us an email at opensource+videoagent@newrelic.com.

# License

This project is distributed under the [Apache 2.0](https://apache.org/licenses/LICENSE-2.0.txt) License.

The video-core also uses source code from third-party libraries. Full details on which libraries are used and the terms under which they are licensed can be found in the [third-party notices document](THIRD_PARTY_NOTICES.md).