Skip to content

Commit

Permalink
feat: add client upload time
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Oct 18, 2023
1 parent 7c16ef5 commit df01e20
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/analytics-browser-test/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ describe('integration', () => {
let apiKey = '';
let client = amplitude.createInstance();

const event_upload_time = '2023-01-01T12:00:00:000Z';
Date.prototype.toISOString = jest.fn(() => event_upload_time);

beforeEach(() => {
client = amplitude.createInstance();
apiKey = UUID();
Expand Down Expand Up @@ -145,6 +148,7 @@ describe('integration', () => {

expect(requestBody1).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
device_id: deviceId,
Expand Down Expand Up @@ -880,6 +884,7 @@ describe('integration', () => {
setTimeout(() => {
expect(payload).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
// This is a `session_end` event for the previous session
Expand Down Expand Up @@ -1104,6 +1109,7 @@ describe('integration', () => {
expect(payload.events[4].session_id).toEqual(payload.events[5].session_id);
expect(payload).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
device_id: uuid,
Expand Down Expand Up @@ -1290,6 +1296,7 @@ describe('integration', () => {
setTimeout(() => {
expect(payload).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
device_id: uuid,
Expand Down Expand Up @@ -1474,6 +1481,7 @@ describe('integration', () => {
setTimeout(() => {
expect(payload).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
device_id: uuid,
Expand Down Expand Up @@ -1572,6 +1580,7 @@ describe('integration', () => {
setTimeout(() => {
expect(payload).toEqual({
api_key: apiKey,
client_upload_time: event_upload_time,
events: [
{
device_id: uuid,
Expand Down
1 change: 1 addition & 0 deletions packages/analytics-core/src/plugins/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class Destination implements DestinationPlugin {
options: {
min_id_length: this.config.minIdLength,
},
client_upload_time: new Date().toISOString(),
};

try {
Expand Down
44 changes: 44 additions & 0 deletions packages/analytics-core/test/plugins/destination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,50 @@ describe('destination', () => {
});

describe('send', () => {
test('should include client upload time', async () => {
const destination = new Destination();
const callback = jest.fn();
const event = {
event_type: 'event_type',
};
const context = {
attempts: 0,
callback,
event,
timeout: 0,
};
const event_upload_time = '2023-01-01T12:00:00:000Z';
Date.prototype.toISOString = jest.fn().mockReturnValueOnce(event_upload_time);

const transportProvider = {
send: jest.fn().mockImplementationOnce((_url: string, payload: Payload) => {
expect(payload.client_upload_time).toBe(event_upload_time);
return Promise.resolve({
status: Status.Success,
statusCode: 200,
body: {
eventsIngested: 1,
payloadSizeBytes: 1,
serverUploadTime: 1,
},
});
}),
};
await destination.setup({
...useDefaultConfig(),
transportProvider,
apiKey: API_KEY,
minIdLength: 10,
});
await destination.send([context]);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith({
event,
code: 200,
message: SUCCESS_MESSAGE,
});
});

test('should include min id length', async () => {
const destination = new Destination();
const callback = jest.fn();
Expand Down
1 change: 1 addition & 0 deletions packages/analytics-types/src/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Payload {
api_key: string;
events: readonly Event[];
options?: PayloadOptions;
client_upload_time?: string;
}

0 comments on commit df01e20

Please sign in to comment.