Skip to content

Commit ffee5f3

Browse files
authored
Remove default CustomEvent dispatcher (v4.0.0) (#35)
* Remove default custom-event behavior (replace with GTM dataLayer[] push) * Add tests for console.error in class decorator * Build * docs * 4.0.0 * Remove roadmap (that's what GH Issues are for :) )
1 parent aa263ee commit ffee5f3

7 files changed

+45
-46
lines changed

README.md

+4-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## Installation
1010

1111
```
12-
npm install --save nytm/nyt-react-tracking#v3.0.0
12+
npm install --save nytm/nyt-react-tracking#v4.0.0
1313
```
1414

1515
(Or whatever is the [latest version](https://github.com/nytm/nyt-react-tracking/releases))
@@ -18,7 +18,7 @@ npm install --save nytm/nyt-react-tracking#v3.0.0
1818
`@track()` expects two arguments, `trackingData` and `options`.
1919
- `trackingData` represents the data to be tracked (or a function returning that data)
2020
- `options` is an optional object that accepts three properties:
21-
- `dispatch`, which is a function to use instead of the default CustomEvent dispatch behavior. See the section on custom `dispatch()` later in this document.
21+
- `dispatch`, which is a function to use instead of the default dispatch behavior. See the section on custom `dispatch()` later in this document.
2222
- `dispatchOnMount`, when set to `true`, dispatches the tracking data when the component mounts to the DOM. When provided as a function will be called on componentDidMount with all of the tracking context data as the only argument.
2323
- `process`, which is a function that can be defined once on some top-level component, used for selectively dispatching tracking events based on each component's tracking data. See more details later in this document.
2424

@@ -93,9 +93,9 @@ This is also how you would use this module without `@decorators`, although this
9393

9494
### Custom `options.dispatch()` for tracking data
9595

96-
By default, data tracking objects are dispatched as a CustomEvent on `document` (see [src/dispatchTrackingEvent.js](src/dispatchTrackingEvent.js)). You can override this by passing in a dispatch function as a second parameter to the tracking decorator `{ dispatch: fn() }` on some top-level component high up in your app (typically some root-level component that wraps your entire app).
96+
By default, data tracking objects are pushed to `window.dataLayer[]` (see [src/dispatchTrackingEvent.js](src/dispatchTrackingEvent.js)). This is a good default if you use Google Tag Manager. You can override this by passing in a dispatch function as a second parameter to the tracking decorator `{ dispatch: fn() }` on some top-level component high up in your app (typically some root-level component that wraps your entire app).
9797

98-
For example, to push objects to `window.dataLayer[]` (e.g. for Google Tag Manager) instead, you would decorate your top-level `<App />` component like this:
98+
For example, to push objects to `window.myCustomDataLayer[]` (e.g. for Google Tag Manager) instead, you would decorate your top-level `<App />` component like this:
9999

100100
```js
101101
import React, { Component } from 'react';
@@ -266,10 +266,3 @@ Note that there are no restrictions on the objects that are passed in to the dec
266266
**The format for the tracking data object is a contract between your app and the ultimate consumer of the tracking data.**
267267
268268
This library simply merges the tracking data objects together (as it flows through your app's React component hierarchy) into a single object that's ultimately sent to the tracking library.
269-
270-
271-
## Roadmap
272-
273-
- Integration with [tracking-schema](https://github.com/nytm/tracking-schema) to provide developer warnings/errors on invalid data objects
274-
- DataLayer adapters (so that where the data goes can vary by app, e.g. to EventTracker or Google Analytics etc.)
275-
- Babel plugin ?

build/dispatchTrackingEvent.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
'use strict';
1+
"use strict";
22

33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66
exports.default = dispatchTrackingEvent;
7-
8-
var _customEvent = require('custom-event');
9-
10-
var _customEvent2 = _interopRequireDefault(_customEvent);
11-
12-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13-
147
function dispatchTrackingEvent(data) {
15-
document.dispatchEvent(new _customEvent2.default('FirehoseTrackingEvent', {
16-
detail: data
17-
}));
8+
(window.dataLayer = window.dataLayer || []).push(data);
189
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nyt/nyt-react-tracking",
3-
"version": "3.0.0",
3+
"version": "4.0.0",
44
"description": "Declarative tracking for React apps.",
55
"author": "The New York Times",
66
"contributors": [
@@ -47,7 +47,6 @@
4747
],
4848
"homepage": "https://github.com/nytm/nyt-react-tracking",
4949
"dependencies": {
50-
"custom-event": "^1.0.1",
5150
"lodash.merge": "^4.6.0"
5251
},
5352
"peerDependencies": {
+19-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
const mockCustomEvent = jest.fn();
2-
jest.setMock('custom-event', mockCustomEvent);
1+
import dispatchTrackingEvent from '../dispatchTrackingEvent';
32

4-
const mockDispatchEvent = jest.fn();
5-
global.document.dispatchEvent = mockDispatchEvent;
3+
const testEventData = { test: 'magik' };
64

75
describe('dispatchTrackingEvent', () => {
8-
// eslint-disable-next-line global-require
9-
const dispatchTrackingEvent = require('../dispatchTrackingEvent').default;
6+
beforeEach(() => {
7+
window.dataLayer = undefined;
8+
});
109

1110
it('exports a function', () => {
1211
expect(typeof dispatchTrackingEvent).toBe('function');
1312
});
1413

15-
it('properly dispatches custom event', () => {
16-
const testEventData = {};
14+
it('will create window.dataLayer[] if it does not exit', () => {
15+
expect(window.dataLayer).not.toBeDefined();
16+
17+
dispatchTrackingEvent(testEventData);
18+
19+
expect(window.dataLayer).toEqual([testEventData]);
20+
});
21+
22+
it('will push to window.dataLayer[] if it exists', () => {
23+
expect(window.dataLayer).not.toBeDefined();
24+
1725
dispatchTrackingEvent(testEventData);
26+
expect(window.dataLayer).toEqual([testEventData]);
1827

19-
expect(mockDispatchEvent).toHaveBeenCalled();
20-
expect(mockCustomEvent).toHaveBeenCalledWith('FirehoseTrackingEvent', {
21-
detail: testEventData,
22-
});
28+
dispatchTrackingEvent(testEventData);
29+
expect(window.dataLayer).toEqual([testEventData, testEventData]);
2330
});
2431
});

src/__tests__/e2e.test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react/no-multi-comp,react/prop-types,react/prefer-stateless-function */
22
import React from 'react';
3-
import { mount } from 'enzyme';
3+
import { shallow, mount } from 'enzyme';
44

55
const dispatchTrackingEvent = jest.fn();
66
jest.setMock('../dispatchTrackingEvent', dispatchTrackingEvent);
@@ -361,4 +361,21 @@ describe('e2e', () => {
361361
});
362362
expect(dispatch).toHaveBeenCalledTimes(2); // pageview event and simulated button click
363363
});
364+
365+
it('logs a console error when there is already a process defined on context', () => {
366+
global.console.error = jest.fn();
367+
const process = () => {};
368+
const context = { tracking: { process } };
369+
370+
@track({}, { process })
371+
class TestComponent extends React.Component {
372+
render() {
373+
return <div />;
374+
}
375+
}
376+
377+
shallow(<TestComponent />, { context });
378+
379+
expect(global.console.error).toHaveBeenCalledTimes(1);
380+
});
364381
});

src/dispatchTrackingEvent.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import CustomEvent from 'custom-event';
2-
31
export default function dispatchTrackingEvent(data) {
4-
document.dispatchEvent(new CustomEvent('FirehoseTrackingEvent', {
5-
detail: data,
6-
}));
2+
(window.dataLayer = window.dataLayer || []).push(data);
73
}

yarn.lock

-4
Original file line numberDiff line numberDiff line change
@@ -1224,10 +1224,6 @@ [email protected], "cssom@>= 0.3.2 < 0.4.0":
12241224
dependencies:
12251225
cssom "0.3.x"
12261226

1227-
custom-event@^1.0.1:
1228-
version "1.0.1"
1229-
resolved "https://registry.yarnpkg.com/custom-event/-/custom-event-1.0.1.tgz#5d02a46850adf1b4a317946a3928fccb5bfd0425"
1230-
12311227
d@1:
12321228
version "1.0.0"
12331229
resolved "https://registry.yarnpkg.com/d/-/d-1.0.0.tgz#754bb5bfe55451da69a58b94d45f4c5b0462d58f"

0 commit comments

Comments
 (0)