Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Add config for omitting the batch timestamp #922

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ export default function APIClient(
mpInstance._Store.integrationDelayTimeoutStart,
Date.now()
);

// set the batch timestamp override in the store if provided as an event option
// setting this to undefined/null will cause no override to be applied
if (options.hasOwnProperty('batchTimestampUnixtimeMsOverride')) {
mpInstance._Store.batchTimestampUnixtimeMsOverride = options.batchTimestampUnixtimeMsOverride;
}

// We queue events if there is no MPID (MPID is null, or === 0), or there are integrations that that require this to stall because integration attributes
// need to be set, or if we are still fetching the config (self hosted only), and so require delaying events
if (
Expand Down
23 changes: 23 additions & 0 deletions src/cookieSyncManager.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MPID } from "@mparticle/web-sdk";
import { Dictionary } from "./utils";
import { IConsentRules } from "./consent";

export interface ICookieSyncManager {
attemptCookieSync: (previousMPID: MPID, mpid: MPID, mpidIsNotInCookies: boolean) => void;
performCookieSync: (
url: string,
moduleId: number,
mpid: MPID,
cookieSyncDates: Dictionary<number>,
filteringConsentRuleValues: IConsentRules,
mpidIsNotInCookies: boolean,
requiresConsent: boolean
) => void;
replaceAmpWithAmpersand: (string: string) => string;
replaceMPID: (string: string, mpid: MPID) => string;

/**
* @deprecated replaceAmp has been deprecated, use replaceAmpersandWithAmp instead
*/
replaceAmp: (string: string) => string;
}
9 changes: 8 additions & 1 deletion src/cookieSyncManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ export default function cookieSyncManager(mpInstance) {
};

// Private
// TODO: Rename function to replaceAmpWithAmpersand
/**
* @deprecated replaceAmp has been deprecated, use replaceAmpersandWithAmp instead
*/
this.replaceAmp = function(string) {
return this.replaceAmpWithAmpersand(string);
};

// Private
this.replaceAmpWithAmpersand = function(string) {
return string.replace(/&amp;/g, '&');
};

Expand Down
17 changes: 17 additions & 0 deletions src/mockBatchCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import { convertEvents } from './sdkToEventsApiConverter';
import * as EventsApi from '@mparticle/event-models';
import { Batch } from '@mparticle/event-models';
import { IMPSideloadedKit } from './sideloadedKit';
import { IStore, SDKConfig } from './store';

const mockFunction = function() {
return null;
};
export default class _BatchValidator {
private configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>;
private storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>;

constructor({
configOverride = {},
storeOverride = {}
}: {
configOverride?: Partial<Pick<SDKConfig, 'omitBatchTimestamp'>>,
storeOverride?: Partial<Pick<IStore, 'batchTimestampUnixtimeMsOverride'>>
} = {}) {
this.configOverride = configOverride
this.storeOverride = storeOverride
}

private getMPInstance() {
return ({
// Certain Helper, Store, and Identity properties need to be mocked to be used in the `returnBatch` method
Expand Down Expand Up @@ -87,7 +102,9 @@ export default class _BatchValidator {
SDKConfig: {
isDevelopmentMode: false,
onCreateBatch: mockFunction,
omitBatchTimestamp: this.configOverride?.omitBatchTimestamp,
},
batchTimestampUnixtimeMsOverride: this.storeOverride?.batchTimestampUnixtimeMsOverride
},
config: null,
eCommerce: null,
Expand Down
3 changes: 3 additions & 0 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from './identity-user-interfaces';
import { IIdentityType } from './types.interfaces';
import IntegrationCapture from './integrationCapture';
import { ICookieSyncManager } from './cookieSyncManager.interfaces';

// TODO: Resolve this with version in @mparticle/web-sdk
export type SDKEventCustomFlags = Dictionary<any>;
Expand Down Expand Up @@ -158,6 +159,7 @@ export interface MParticleWebSDK {
Logger: SDKLoggerApi;
MPSideloadedKit: IMPSideloadedKit;
_APIClient: any; // TODO: Set up API Client
_CookieSyncManager: ICookieSyncManager;
_Store: IStore;
_Forwarders: any;
_Helpers: SDKHelpersApi;
Expand Down Expand Up @@ -248,6 +250,7 @@ export interface SDKInitConfig

workspaceToken?: string;
isDevelopmentMode?: boolean;
omitBatchTimestamp?: boolean;

// https://go.mparticle.com/work/SQDSDKS-6460
identityCallback?: IdentityCallback;
Expand Down
19 changes: 17 additions & 2 deletions src/sdkToEventsApiConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SDKCCPAConsentState,
} from './consent';
import Types from './types';
import { isEmpty } from './utils';
import { isEmpty, isNumber } from './utils';
import { ISDKUserIdentity } from './identity-user-interfaces';
import { SDKIdentityTypeEnum } from './identity.interfaces';

Expand Down Expand Up @@ -56,10 +56,25 @@ export function convertEvents(
currentConsentState = user.getConsentState();
}

// determine what timestamp, if any, to use for the batch
const { omitBatchTimestamp } = mpInstance._Store.SDKConfig;
const { batchTimestampUnixtimeMsOverride } = mpInstance._Store;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that you're puling this from _Store as we discussed, but I don't see any code path where batchTimestampUnixtimeMsOverride is being added to the actual _Store. If I remember correctly, we discussed that the override would be added as an eventOption.

I think within APIClient.sendEventToServer, you can pass in batchTimestampUnixtimeMsOverride through _options and use that to set it in the _Store, where it can then be picked up here.

Copy link
Member

@rmi22186 rmi22186 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder that it should be added to each of logPageView, logEvent, and logProductAction.

Per your and my comments, I am striking out the above.


let timestamp_unixtime_ms: number | null;

if (isNumber(batchTimestampUnixtimeMsOverride)) {
timestamp_unixtime_ms = batchTimestampUnixtimeMsOverride;
} else if (omitBatchTimestamp) {
timestamp_unixtime_ms = null;
} else {
timestamp_unixtime_ms = new Date().getTime();
}


const upload: EventsApi.Batch = {
source_request_id: mpInstance._Helpers.generateUniqueId(),
mpid,
timestamp_unixtime_ms: new Date().getTime(),
timestamp_unixtime_ms,
environment: lastEvent.Debug
? EventsApi.BatchEnvironmentEnum.development
: EventsApi.BatchEnvironmentEnum.production,
Expand Down
6 changes: 6 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface SDKConfig {
webviewBridgeName?: string;
workspaceToken?: string;
requiredWebviewBridgeName?: string;
omitBatchTimestamp?: boolean;
}

function createSDKConfig(config: SDKInitConfig): SDKConfig {
Expand Down Expand Up @@ -182,6 +183,7 @@ export interface IStore {
integrationDelayTimeoutStart: number; // UNIX Timestamp
webviewBridgeEnabled?: boolean;
wrapperSDKInfo: WrapperSDKInfo;
batchTimestampUnixtimeMsOverride?: number

persistenceData?: IPersistenceMinified;

Expand Down Expand Up @@ -477,6 +479,10 @@ export default function Store(
this.SDKConfig.onCreateBatch = undefined;
}
}

if (config.hasOwnProperty('omitBatchTimestamp')) {
this.SDKConfig.omitBatchTimestamp = config.omitBatchTimestamp;
}
}

this._getFromPersistence = <T>(mpid: MPID, key: string): T | null => {
Expand Down
30 changes: 30 additions & 0 deletions test/src/tests-apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,34 @@ describe('Api Client', () => {

done();
});

[undefined, null, new Date().getTime()].forEach(
(batchTimestampUnixtimeMsOverride) => {
it("sendEventToServer should update batchTimestampUnixtimeMsOverride", (done) => {
const event = {
messageType: Types.MessageType.PageEvent,
name: "foo page",
data: { "foo-attr": "foo-val" },
eventType: Types.EventType.Navigation,
customFlags: { "foo-flag": "foo-flag-val" },
};

const options = { batchTimestampUnixtimeMsOverride };

expect(mParticle.getInstance()._Store).to.be.ok;

mParticle
.getInstance()
._APIClient.sendEventToServer(event, options);

expect(
mParticle.getInstance()._Store.batchTimestampUnixtimeMsOverride,
).to.equal(batchTimestampUnixtimeMsOverride);

done();
});
},
);


});
69 changes: 65 additions & 4 deletions test/src/tests-mockBatchCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { BaseEvent } from '../../src/sdkRuntimeModels';
import { expect } from 'chai';

describe('Create a batch from a base event', () => {
const batchValidator = new _BatchValidator();
const baseEvent: BaseEvent = {
messageType: 4,
name: 'testEvent'
}

it('creates a batch with base event ', done => {
const now = new Date().getTime();
const batchValidator = new _BatchValidator()

let batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('environment').equal('production');
expect(batch).to.have.property('source_request_id').equal('mockId');
expect(batch).to.have.property('mpid').equal('0');
expect(batch).to.have.property('timestamp_unixtime_ms')
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
expect(batch).to.have.property('mp_deviceid');
expect(batch).to.have.property('sdk_version')
expect(batch).to.have.property('application_info');
Expand Down Expand Up @@ -47,12 +49,71 @@ describe('Create a batch from a base event', () => {
batch = batchValidator.returnBatch(baseEvent);
expect(batch.events[0].data).to.have.property('custom_attributes');
expect(batch.events[0].data.custom_attributes).to.have.property('attrFoo', 'attrBar');

baseEvent.customFlags = { flagFoo: 'flagBar' }
batch = batchValidator.returnBatch(baseEvent);
expect(batch.events[0].data).to.have.property('custom_flags');
expect(batch.events[0].data.custom_flags).to.have.property('flagFoo', 'flagBar');

done();
});

[undefined, null, false, true].forEach(omitBatchTimestamp => {
it(`respects an omitBatchTimestamp config value of ${omitBatchTimestamp}`, done => {
const now = new Date().getTime();
const batchValidator = new _BatchValidator({
configOverride: {omitBatchTimestamp}
});

const batch = batchValidator.returnBatch(baseEvent);

if (omitBatchTimestamp) {
expect(batch).to.have.property('timestamp_unixtime_ms', null);
} else {
expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);
}

done();
});
})

it(`can use a batch timestamp override value`, done => {
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
const batchValidator = new _BatchValidator({
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}
});
const batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);

done();
});

it(`a batch timestamp override takes precedence over the sdk config`, done => {
const oneDayAgo = new Date().getTime() - (24 * 3600 * 1000);
const batchValidator = new _BatchValidator({
configOverride: {omitBatchTimestamp: true},
storeOverride: {batchTimestampUnixtimeMsOverride: oneDayAgo}
});
const batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(oneDayAgo);

done();
});

[undefined, null].forEach(batchTimestampUnixtimeMsOverride => {
it(`a non-number batch timestamp override of ${batchTimestampUnixtimeMsOverride} is not sent`, done => {
const now = new Date().getTime();

const batchValidator = new _BatchValidator({
storeOverride: {batchTimestampUnixtimeMsOverride}
});
const batch = batchValidator.returnBatch(baseEvent);

expect(batch).to.have.property('timestamp_unixtime_ms').greaterThanOrEqual(now);

done();
});
})
});
Loading