mirror of
https://github.com/jlengrand/adyen-web.git
synced 2026-03-10 08:01:22 +00:00
Telemetry - Adding amount currency and value (#2005)
This commit is contained in:
committed by
GitHub
parent
cb99905970
commit
eccbd9c8d3
@@ -1,6 +1,7 @@
|
||||
import Analytics from './Analytics';
|
||||
import collectId from '../Services/analytics/collect-id';
|
||||
import postTelemetry from '../Services/analytics/post-telemetry';
|
||||
import { PaymentAmountExtended } from '../../types';
|
||||
|
||||
jest.mock('../Services/analytics/collect-id');
|
||||
jest.mock('../Services/analytics/post-telemetry');
|
||||
@@ -8,6 +9,8 @@ jest.mock('../Services/analytics/post-telemetry');
|
||||
const mockedCollectId = collectId as jest.Mock;
|
||||
const mockedPostTelemetry = postTelemetry as jest.Mock;
|
||||
|
||||
let amount: PaymentAmountExtended;
|
||||
|
||||
describe('Analytics', () => {
|
||||
const collectIdPromiseMock = jest.fn(() => Promise.resolve('123456'));
|
||||
const logTelemetryPromiseMock = jest.fn(request => Promise.resolve(request));
|
||||
@@ -19,24 +22,26 @@ describe('Analytics', () => {
|
||||
mockedPostTelemetry.mockReset();
|
||||
mockedPostTelemetry.mockImplementation(() => logTelemetryPromiseMock);
|
||||
logTelemetryPromiseMock.mockClear();
|
||||
|
||||
amount = { value: 50000, currency: 'USD' };
|
||||
});
|
||||
|
||||
test('Creates an Analytics module with defaultProps', () => {
|
||||
const analytics = new Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
expect(analytics.props.enabled).toBe(true);
|
||||
expect(analytics.props.telemetry).toBe(true);
|
||||
expect(collectIdPromiseMock).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('Calls the collectId endpoint by default (telemetry enabled)', () => {
|
||||
const analytics = new Analytics({ loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
expect(collectIdPromiseMock).not.toHaveBeenCalled();
|
||||
analytics.send({});
|
||||
expect(collectIdPromiseMock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Will not call the collectId endpoint if telemetry is disabled', () => {
|
||||
const analytics = new Analytics({ analytics: { telemetry: false }, loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: { telemetry: false }, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
expect(collectIdPromiseMock).not.toHaveBeenCalled();
|
||||
analytics.send({});
|
||||
expect(collectIdPromiseMock).not.toHaveBeenCalled();
|
||||
@@ -46,7 +51,7 @@ describe('Analytics', () => {
|
||||
const event = {
|
||||
eventData: 'test'
|
||||
};
|
||||
const analytics = new Analytics({ loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: {}, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
analytics.send(event);
|
||||
|
||||
expect(collectIdPromiseMock).toHaveBeenCalled();
|
||||
@@ -61,7 +66,7 @@ describe('Analytics', () => {
|
||||
const event = {
|
||||
eventData: 'test'
|
||||
};
|
||||
const analytics = new Analytics({ analytics: { payload }, loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: { payload }, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
|
||||
analytics.send(event);
|
||||
|
||||
@@ -71,7 +76,7 @@ describe('Analytics', () => {
|
||||
});
|
||||
|
||||
test('Should not fire any calls if analytics is disabled', () => {
|
||||
const analytics = new Analytics({ analytics: { enabled: false }, loadingContext: '', locale: '', clientKey: '' });
|
||||
const analytics = new Analytics({ analytics: { enabled: false }, loadingContext: '', locale: '', clientKey: '', amount });
|
||||
|
||||
analytics.send({});
|
||||
expect(collectIdPromiseMock).not.toHaveBeenCalled();
|
||||
|
||||
@@ -4,6 +4,8 @@ import collectId from '../Services/analytics/collect-id';
|
||||
import EventsQueue from './EventsQueue';
|
||||
import { CoreOptions } from '../types';
|
||||
|
||||
export type AnalyticsProps = Pick<CoreOptions, 'loadingContext' | 'locale' | 'clientKey' | 'analytics' | 'amount'>;
|
||||
|
||||
class Analytics {
|
||||
private static defaultProps = {
|
||||
enabled: true,
|
||||
@@ -19,10 +21,10 @@ class Analytics {
|
||||
private readonly queue = new EventsQueue();
|
||||
public readonly collectId;
|
||||
|
||||
constructor({ loadingContext, locale, clientKey, analytics }: CoreOptions) {
|
||||
constructor({ loadingContext, locale, clientKey, analytics, amount }: AnalyticsProps) {
|
||||
this.props = { ...Analytics.defaultProps, ...analytics };
|
||||
this.logEvent = logEvent({ loadingContext, locale });
|
||||
this.logTelemetry = postTelemetry({ loadingContext, locale, clientKey });
|
||||
this.logTelemetry = postTelemetry({ loadingContext, locale, clientKey, amount });
|
||||
this.collectId = collectId({ loadingContext, clientKey, experiments: this.props.experiments });
|
||||
|
||||
const { telemetry, enabled } = this.props;
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { httpPost } from '../http';
|
||||
import { AnalyticsProps } from '../../Analytics/Analytics';
|
||||
|
||||
type Config = Pick<AnalyticsProps, 'loadingContext' | 'locale' | 'clientKey' | 'amount'>;
|
||||
|
||||
/**
|
||||
* Log event to Adyen
|
||||
* @param config -
|
||||
*/
|
||||
const logTelemetry = config => event => {
|
||||
const logTelemetry = (config: Config) => (event: any) => {
|
||||
if (!config.clientKey) return Promise.reject();
|
||||
|
||||
const options = {
|
||||
@@ -14,6 +17,8 @@ const logTelemetry = config => event => {
|
||||
};
|
||||
|
||||
const telemetryEvent = {
|
||||
amountValue: config.amount?.value,
|
||||
amountCurrency: config.amount?.currency,
|
||||
version: process.env.VERSION,
|
||||
channel: 'Web',
|
||||
locale: config.locale,
|
||||
|
||||
Reference in New Issue
Block a user