Skip to content

Commit

Permalink
Enabling Telemetry (#1603)
Browse files Browse the repository at this point in the history
* feat: enabling telemetry

* fix: updating release link

* Updated link to docs
  • Loading branch information
ribeiroguilherme authored May 23, 2022
1 parent f3f9c1d commit cec16f3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ To run the development environment:
yarn start
```

## Analytics and data tracking
Starting [v5.16.0](https://github.com/Adyen/adyen-web/releases/tag/v5.16.0) the Drop-in and Components integrations contain analytics and tracking features that are turned on by default. Find out more about [what we track and how you can control it](https://docs.adyen.com/online-payments/analytics-and-data-tracking).

## Contributing

We merge every pull request into the `master` branch. We aim to keep `master` in good shape, which allows us to release a new version whenever we need to.
Expand Down
36 changes: 25 additions & 11 deletions packages/lib/src/core/Analytics/Analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,56 @@ describe('Analytics', () => {
const analytics = new Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '' });
expect(analytics.props.enabled).toBe(true);
expect(analytics.props.telemetry).toBe(true);
expect(analytics.props.conversion).toBe(false);
expect(collectIdPromiseMock).toHaveLength(0);
});

test('Calls the collectId endpoint if conversion is enabled after the first telemetry call', () => {
const analytics = new Analytics({ analytics: { conversion: true }, loadingContext: '', locale: '', clientKey: '' });
test('Calls the collectId endpoint by default (telemetry enabled)', () => {
const analytics = new Analytics({ loadingContext: '', locale: '', clientKey: '' });
expect(collectIdPromiseMock).not.toHaveBeenCalled();
analytics.send({});
expect(collectIdPromiseMock).toHaveBeenCalled();
});

test('Will not call the collectId endpoint if conversion is disabled', () => {
const analytics = new Analytics({ analytics: { conversion: false }, loadingContext: '', locale: '', clientKey: '' });
test('Will not call the collectId endpoint if telemetry is disabled', () => {
const analytics = new Analytics({ analytics: { telemetry: false }, loadingContext: '', locale: '', clientKey: '' });
expect(collectIdPromiseMock).not.toHaveBeenCalled();
analytics.send({});
expect(collectIdPromiseMock).not.toHaveBeenCalled();
});

test('Sends an event', () => {
test('Sends an event', async () => {
const event = {
eventData: 'test'
};
const analytics = new Analytics({ analytics: { conversion: false }, loadingContext: '', locale: '', clientKey: '' });
const analytics = new Analytics({ loadingContext: '', locale: '', clientKey: '' });
analytics.send(event);
expect(logTelemetryPromiseMock).toHaveBeenCalledWith({ ...event, checkoutAttemptId: null });

expect(collectIdPromiseMock).toHaveBeenCalled();
await Promise.resolve(); // wait for the next tick
expect(logTelemetryPromiseMock).toHaveBeenCalledWith({ ...event, checkoutAttemptId: '123456' });
});

test('Adds the fields in the payload', () => {
test('Adds the fields in the payload', async () => {
const payload = {
payloadData: 'test'
};
const event = {
eventData: 'test'
};
const analytics = new Analytics({ analytics: { conversion: false, payload }, loadingContext: '', locale: '', clientKey: '' });
const analytics = new Analytics({ analytics: { payload }, loadingContext: '', locale: '', clientKey: '' });

analytics.send(event);
expect(logTelemetryPromiseMock).toHaveBeenCalledWith({ ...payload, ...event, checkoutAttemptId: null });

expect(collectIdPromiseMock).toHaveBeenCalled();
await Promise.resolve(); // wait for the next tick
expect(logTelemetryPromiseMock).toHaveBeenCalledWith({ ...payload, ...event, checkoutAttemptId: '123456' });
});

test('Should not fire any calls if analytics is disabled', () => {
const analytics = new Analytics({ analytics: { enabled: false }, loadingContext: '', locale: '', clientKey: '' });

analytics.send({});
expect(collectIdPromiseMock).not.toHaveBeenCalled();
expect(logTelemetryPromiseMock).not.toHaveBeenCalled();
});
});
15 changes: 7 additions & 8 deletions packages/lib/src/core/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ class Analytics {
private static defaultProps = {
enabled: true,
telemetry: true,
conversion: false,
checkoutAttemptId: null,
experiments: []
};

public checkoutAttemptId = null;
public checkoutAttemptId: string = null;
public props;
private readonly logEvent;
private readonly logTelemetry;
Expand All @@ -26,8 +25,8 @@ class Analytics {
this.logTelemetry = postTelemetry({ loadingContext, locale, clientKey });
this.collectId = collectId({ loadingContext, clientKey, experiments: this.props.experiments });

const { conversion, enabled } = this.props;
if (conversion === true && enabled === true) {
const { telemetry, enabled } = this.props;
if (telemetry === true && enabled === true) {
if (this.props.checkoutAttemptId) {
// handle prefilled checkoutAttemptId
this.checkoutAttemptId = this.props.checkoutAttemptId;
Expand All @@ -37,10 +36,10 @@ class Analytics {
}

send(event) {
const { conversion, enabled, payload, telemetry } = this.props;
const { enabled, payload, telemetry } = this.props;

if (enabled === true) {
if (conversion === true && !this.checkoutAttemptId) {
if (telemetry === true && !this.checkoutAttemptId) {
// fetch a new checkoutAttemptId if none is already available
this.collectId().then(checkoutAttemptId => {
this.checkoutAttemptId = checkoutAttemptId;
Expand All @@ -51,10 +50,10 @@ class Analytics {
if (telemetry === true) {
const telemetryTask = checkoutAttemptId =>
this.logTelemetry({ ...event, ...(payload && { ...payload }), checkoutAttemptId }).catch(() => {});

this.queue.add(telemetryTask);

// Not waiting for checkoutAttemptId
if (!conversion || this.checkoutAttemptId) {
if (this.checkoutAttemptId) {
this.queue.run(this.checkoutAttemptId);
}
}
Expand Down
5 changes: 0 additions & 5 deletions packages/lib/src/core/Analytics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export interface AnalyticsOptions {
*/
telemetry?: boolean;

/**
* Enable/Disable conversion events
*/
conversion?: boolean;

/**
* Reuse a previous checkoutAttemptId from a previous page
*/
Expand Down

0 comments on commit cec16f3

Please sign in to comment.