From cc85fa1c1565bb3c9cdf34279b6666a5e7f511a1 Mon Sep 17 00:00:00 2001 From: Alexander Sapountzis Date: Wed, 17 Jun 2026 17:20:02 -0400 Subject: [PATCH] feat: remove batch forwarding to kits Remove the client-side sendBatchToForwarders path that pushed event batches into kit processBatch. Server-side mParticle integration now handles this data, making the client-side batch-stream forwarding redundant. - Drop the sendBatchToForwarders() call site in batchUploader.ts - Remove the sendBatchToForwarders method and its now-unused batch filtering helper imports from forwarders.js - Remove the associated test coverage in batchUploader.spec.ts and tests-forwarders.ts --- src/batchUploader.ts | 12 - src/forwarders.js | 58 --- test/jest/batchUploader.spec.ts | 226 -------- test/src/tests-forwarders.ts | 885 -------------------------------- 4 files changed, 1181 deletions(-) diff --git a/src/batchUploader.ts b/src/batchUploader.ts index 8b1ab4974..662a7fc9d 100644 --- a/src/batchUploader.ts +++ b/src/batchUploader.ts @@ -368,18 +368,6 @@ export class BatchUploader { mpInstance ); - if (uploadBatchObject) { - try { - mpInstance._Forwarders.sendBatchToForwarders( - uploadBatchObject - ); - } catch (e) { - mpInstance.Logger.error( - `Error forwarding batch to kits. ${e}` - ); - } - } - const onCreateBatchCallback = mpInstance._Store.SDKConfig.onCreateBatch; diff --git a/src/forwarders.js b/src/forwarders.js index 889e339dc..8829be52c 100644 --- a/src/forwarders.js +++ b/src/forwarders.js @@ -8,9 +8,6 @@ import { isBlockedByEventFilter, filterEventAttributes, filterUserIdentities, - isBatchEventAllowed, - filterBatchEventAttributes, - filterBatchIdentities, } from './forwarder-utils'; const { Modify, Identify, Login, Logout } = Constants.IdentityMethods; @@ -266,61 +263,6 @@ export default function Forwarders(mpInstance, kitBlocker) { } }; - this.sendBatchToForwarders = function(batch) { - if ( - mpInstance._Store.webviewBridgeEnabled || - !mpInstance._Store.activeForwarders - ) { - return; - } - - for (const forwarder of mpInstance._Store.activeForwarders) { - if (!forwarder.processBatch) { - continue; - } - - try { - const batchCopy = extend(true, {}, batch); - - if (batchCopy.events) { - batchCopy.events = batchCopy.events.filter(function( - batchEvent - ) { - return isBatchEventAllowed(batchEvent, forwarder); - }); - - batchCopy.events.forEach(function(batchEvent) { - filterBatchEventAttributes(batchEvent, forwarder); - }); - } - - batchCopy.user_identities = filterBatchIdentities( - batchCopy.user_identities, - forwarder.userIdentityFilters - ); - - if (batchCopy.user_attributes) { - batchCopy.user_attributes = KitFilterHelper.filterUserAttributes( - batchCopy.user_attributes, - forwarder.userAttributeFilters - ); - } - - if (!batchCopy.events || batchCopy.events.length === 0) { - continue; - } - - const result = forwarder.processBatch(batchCopy); - - if (result) { - mpInstance.Logger.verbose(result); - } - } catch (e) { - mpInstance.Logger.verbose(e); - } - } - }; - this.handleForwarderUserAttributes = function(functionNameKey, key, value) { if ( (kitBlocker && kitBlocker.isAttributeKeyBlocked(key)) || diff --git a/test/jest/batchUploader.spec.ts b/test/jest/batchUploader.spec.ts index d410a57ee..6ed15a1fe 100644 --- a/test/jest/batchUploader.spec.ts +++ b/test/jest/batchUploader.spec.ts @@ -5,7 +5,6 @@ describe('BatchUploader', () => { let batchUploader: BatchUploader; let mockMPInstance: IMParticleWebSDKInstance; let originalFetch: typeof global.fetch; - let mockSendBatchToForwarders: jest.Mock; beforeEach(() => { const now = Date.now(); @@ -17,7 +16,6 @@ describe('BatchUploader', () => { global.fetch = jest.fn().mockResolvedValue({ ok: true, status: 200 }); // Create a mock mParticle instance with mocked methods for instantiating a BatchUploader - mockSendBatchToForwarders = jest.fn(); mockMPInstance = { _Store: { SDKConfig: { @@ -30,9 +28,6 @@ describe('BatchUploader', () => { createServiceUrl: jest.fn().mockReturnValue('https://mock-url.com'), generateUniqueId: jest.fn().mockReturnValue('mock-uuid'), }, - _Forwarders: { - sendBatchToForwarders: mockSendBatchToForwarders, - }, Identity: { getCurrentUser: jest.fn().mockReturnValue({ getMPID: () => 'test-mpid', @@ -58,227 +53,6 @@ describe('BatchUploader', () => { global.fetch = originalFetch; }); - describe('batch forwarding to kits', () => { - it('should call sendBatchToForwarders for each new batch in createNewBatches', () => { - const mockEvents = [ - { - EventName: 'Test Event', - EventDataType: 4, - MPID: 'test-mpid', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'test-mpid', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - - expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(1); - expect(mockSendBatchToForwarders).toHaveBeenCalledWith( - expect.objectContaining({ - mpid: 'test-mpid', - }) - ); - }); - - it('should call sendBatchToForwarders before onCreateBatch', () => { - const callOrder: string[] = []; - - mockSendBatchToForwarders.mockImplementation(() => { - callOrder.push('sendBatchToForwarders'); - }); - - mockMPInstance._Store.SDKConfig.onCreateBatch = (batch) => { - callOrder.push('onCreateBatch'); - return batch; - }; - - const mockEvents = [ - { - EventName: 'Test Event', - EventDataType: 4, - MPID: 'test-mpid', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'test-mpid', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - - expect(callOrder).toEqual([ - 'sendBatchToForwarders', - 'onCreateBatch', - ]); - }); - - it('should forward batch before onCreateBatch can modify it', () => { - let forwardedEventCount = 0; - - mockSendBatchToForwarders.mockImplementation((batch) => { - forwardedEventCount = batch.events.length; - }); - - mockMPInstance._Store.SDKConfig.onCreateBatch = (batch) => { - batch.modified = true; - batch.events = []; - return batch; - }; - - const mockEvents = [ - { - EventName: 'Test Event', - EventDataType: 4, - MPID: 'test-mpid', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'test-mpid', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - - expect(forwardedEventCount).toBe(1); - }); - - it('should forward batch even when onCreateBatch drops it', () => { - mockMPInstance._Store.SDKConfig.onCreateBatch = () => { - return null; - }; - - (mockMPInstance.Logger as any).warning = jest.fn(); - - const mockEvents = [ - { - EventName: 'Test Event', - EventDataType: 4, - MPID: 'test-mpid', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'test-mpid', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - - expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(1); - }); - - it('should not throw if sendBatchToForwarders errors', () => { - mockSendBatchToForwarders.mockImplementation(() => { - throw new Error('Kit failure'); - }); - - const mockEvents = [ - { - EventName: 'Test Event', - EventDataType: 4, - MPID: 'test-mpid', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'test-mpid', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - expect(() => { - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - }).not.toThrow(); - - expect(mockMPInstance.Logger.error).toHaveBeenCalled(); - }); - - it('should create separate batches per MPID and forward each', () => { - const mockEvents = [ - { - EventName: 'Event User A', - EventDataType: 4, - MPID: 'mpid-a', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - { - EventName: 'Event User B', - EventDataType: 4, - MPID: 'mpid-b', - SessionId: 'session-1', - EventCategory: 1, - Timestamp: Date.now(), - }, - ]; - - const mockUser = { - getMPID: () => 'mpid-a', - getConsentState: () => null, - getUserIdentities: () => ({ userIdentities: {} }), - getAllUserAttributes: () => ({}), - }; - - BatchUploader['createNewBatches']( - mockEvents as any, - mockUser as any, - mockMPInstance - ); - - expect(mockSendBatchToForwarders).toHaveBeenCalledTimes(2); - }); - }); - describe('shouldDebounceAST', () => { it('should return false for first call', () => { // First call should not be debounced diff --git a/test/src/tests-forwarders.ts b/test/src/tests-forwarders.ts index d487090b3..de8b71ba9 100644 --- a/test/src/tests-forwarders.ts +++ b/test/src/tests-forwarders.ts @@ -4019,889 +4019,4 @@ describe('forwarders', function() { }); }); - describe('batch forwarding', () => { - it('should call processBatch on forwarders that implement it', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch = null; - forwarderInstance.processBatch = function(batch) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [{ event_type: 'custom_event' }], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].event_type).to.equal( - 'custom_event' - ); - }); - - it('should not call processBatch on forwarders that do not implement it', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - expect(forwarderInstance.processBatch).to.not.be.ok; - - const fakeBatch = { - events: [{ event_type: 'custom_event' }], - mpid: testMPID, - }; - - expect(() => { - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - }).to.not.throw(); - }); - - it('should still send individual events via process() regardless of processBatch', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let batchReceived = false; - forwarderInstance.processBatch = function() { - batchReceived = true; - }; - - mParticle.logEvent('Test Event'); - - // Individual event should still arrive via process() - expect(forwarderInstance.processCalled).to.equal(true); - expect(forwarderInstance.receivedEvent).to.be.ok; - expect(forwarderInstance.receivedEvent.EventName).to.equal( - 'Test Event' - ); - }); - - it('should not send batches to forwarders when webviewBridgeEnabled is true', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let batchReceived = false; - forwarderInstance.processBatch = function() { - batchReceived = true; - }; - - mParticle.getInstance()._Store.webviewBridgeEnabled = true; - - const fakeBatch = { - events: [{ event_type: 'custom_event' }], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(batchReceived).to.equal(false); - - mParticle.getInstance()._Store.webviewBridgeEnabled = false; - }); - - it('should handle errors in processBatch gracefully', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - forwarderInstance.processBatch = function() { - throw new Error('Kit error'); - }; - - const fakeBatch = { - events: [{ event_type: 'custom_event' }], - mpid: testMPID, - }; - - expect(() => { - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - }).to.not.throw(); - }); - - it('should deep-clone the batch so kit mutations do not corrupt the original', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - window.mParticle.config.kitConfigs.push( - forwarderDefaultConfiguration('MockForwarder') - ); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - forwarderInstance.processBatch = function(batch: any) { - batch.events = []; - batch.mpid = 'mutated'; - }; - - const fakeBatch = { - events: [{ event_type: 'custom_event' }], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(fakeBatch.events.length).to.equal(1); - expect(fakeBatch.mpid).to.equal(testMPID); - }); - - it('should filter blocked events from the batch by event name', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.eventNameFilters = [ - mParticle.generateHash( - mParticle.EventType.Navigation + 'blocked event' - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'blocked event', - custom_event_type: 'navigation', - custom_attributes: {}, - }, - }, - { - event_type: 'custom_event', - data: { - event_name: 'allowed event', - custom_event_type: 'navigation', - custom_attributes: {}, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].data.event_name).to.equal( - 'allowed event' - ); - }); - - it('should filter screen_view events from the batch by screen name', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.screenNameFilters = [ - mParticle.generateHash( - mParticle.EventType.Unknown + 'blocked page' - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'screen_view', - data: { - screen_name: 'blocked page', - custom_attributes: {}, - }, - }, - { - event_type: 'screen_view', - data: { - screen_name: 'allowed page', - custom_attributes: {}, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].data.screen_name).to.equal( - 'allowed page' - ); - }); - - it('should filter commerce events from the batch by event type', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.eventTypeFilters = [ - mParticle.generateHash( - String(CommerceEventType.ProductViewDetail) - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'commerce_event', - data: { - product_action: { - action: 'view_detail', - products: [ - { - id: 'SKU-123', - name: 'Test Product', - price: 19.99, - quantity: 1, - }, - ], - }, - custom_attributes: {}, - }, - }, - { - event_type: 'commerce_event', - data: { - product_action: { - action: 'purchase', - products: [ - { - id: 'SKU-456', - name: 'Other Product', - price: 9.99, - quantity: 2, - }, - ], - }, - custom_attributes: {}, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect( - receivedBatch.events[0].data.product_action.action - ).to.equal('purchase'); - }); - - it('should strip filtered user attributes from the batch', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.userAttributeFilters = [ - mParticle.generateHash('gender') as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'test event', - custom_event_type: 'other', - }, - }, - ], - mpid: testMPID, - user_attributes: { gender: 'male', age: '25' }, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.user_attributes).to.not.have.property( - 'gender' - ); - expect(receivedBatch.user_attributes).to.have.property( - 'age', - '25' - ); - }); - - it('should strip filtered user identities from the batch', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.userIdentityFilters = [ - IdentityType.Email as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'test event', - custom_event_type: 'other', - }, - }, - ], - mpid: testMPID, - user_identities: { - email: 'user@test.com', - customer_id: 'cust-123', - }, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.user_identities).to.not.have.property( - 'email' - ); - expect(receivedBatch.user_identities).to.have.property( - 'customer_id', - 'cust-123' - ); - }); - - it('should strip filtered event attributes from batch events', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.attributeFilters = [ - mParticle.generateHash( - mParticle.EventType.Navigation + - 'test event' + - 'secret' - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'test event', - custom_event_type: 'navigation', - custom_attributes: { - secret: 'hidden', - color: 'blue', - }, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect( - receivedBatch.events[0].data.custom_attributes - ).to.not.have.property('secret'); - expect( - receivedBatch.events[0].data.custom_attributes - ).to.have.property('color', 'blue'); - }); - - it('should skip processBatch when all events are filtered out', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.eventNameFilters = [ - mParticle.generateHash( - mParticle.EventType.Other + 'only event' - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let processBatchCalled = false; - forwarderInstance.processBatch = function() { - processBatchCalled = true; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'only event', - custom_event_type: 'other', - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(processBatchCalled).to.equal(false); - }); - - it('should block batch event when attribute forwarding rule matches and includeOnMatch is false', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.filteringEventAttributeValue = { - eventAttributeName: mParticle - .generateHash('ForwardingRule') - .toString(), - eventAttributeValue: mParticle - .generateHash('Block') - .toString(), - includeOnMatch: false, - }; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'blocked event', - custom_event_type: 'navigation', - custom_attributes: { - ForwardingRule: 'Block', - }, - }, - }, - { - event_type: 'custom_event', - data: { - event_name: 'allowed event', - custom_event_type: 'navigation', - custom_attributes: { - ForwardingRule: 'NoMatch', - }, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].data.event_name).to.equal( - 'allowed event' - ); - }); - - it('should allow batch event when attribute forwarding rule matches and includeOnMatch is true', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.filteringEventAttributeValue = { - eventAttributeName: mParticle - .generateHash('ForwardingRule') - .toString(), - eventAttributeValue: mParticle - .generateHash('Allow') - .toString(), - includeOnMatch: true, - }; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'matching event', - custom_event_type: 'navigation', - custom_attributes: { - ForwardingRule: 'Allow', - }, - }, - }, - { - event_type: 'custom_event', - data: { - event_name: 'non-matching event', - custom_event_type: 'navigation', - custom_attributes: { - ForwardingRule: 'Other', - }, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].data.event_name).to.equal( - 'matching event' - ); - }); - - it('should apply the same event name filter to both individual events and batch events', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.eventNameFilters = [ - mParticle.generateHash( - mParticle.EventType.Navigation + 'blocked event' - ) as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - - // --- Event path: logEvent should be blocked --- - forwarderInstance.receivedEvent = null; - mParticle.logEvent( - 'blocked event', - mParticle.EventType.Navigation - ); - expect(forwarderInstance.receivedEvent).to.not.be.ok; - - // --- Batch path: same event should be blocked --- - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'blocked event', - custom_event_type: 'navigation', - custom_attributes: {}, - }, - }, - { - event_type: 'custom_event', - data: { - event_name: 'allowed event', - custom_event_type: 'navigation', - custom_attributes: {}, - }, - }, - ], - mpid: testMPID, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.events.length).to.equal(1); - expect(receivedBatch.events[0].data.event_name).to.equal( - 'allowed event' - ); - }); - - it('should apply the same user identity filter to both individual events and batch events', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.userIdentityFilters = [ - IdentityType.Email as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - - // --- Event path: email identity should be stripped --- - forwarderInstance.receivedEvent = null; - mParticle.logEvent('test event', mParticle.EventType.Other); - const eventResult = forwarderInstance.receivedEvent; - expect(eventResult).to.be.ok; - expect(eventResult.UserIdentities).to.be.ok; - const hasEmail = eventResult.UserIdentities.some( - (id: any) => id.Type === IdentityType.Email - ); - expect(hasEmail).to.equal(false); - - // --- Batch path: email identity should be stripped --- - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'test event', - custom_event_type: 'other', - }, - }, - ], - mpid: testMPID, - user_identities: { - email: 'user@test.com', - customer_id: 'cust-123', - }, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.user_identities).to.not.have.property( - 'email' - ); - expect(receivedBatch.user_identities).to.have.property( - 'customer_id', - 'cust-123' - ); - }); - - it('should apply the same user attribute filter to both individual events and batch events', async () => { - const mockForwarder = new MockForwarder(); - mockForwarder.register(window.mParticle.config); - const config1 = forwarderDefaultConfiguration( - 'MockForwarder', - 1 - ); - config1.userAttributeFilters = [ - mParticle.generateHash('secret') as unknown as number, - ]; - window.mParticle.config.kitConfigs.push(config1); - - mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasIdentifyReturned); - - const forwarderInstance = window.MockForwarder1.instance; - - // --- Event path: 'secret' attribute should be stripped --- - mParticle - .Identity.getCurrentUser() - .setUserAttribute('secret', 'hidden'); - mParticle - .Identity.getCurrentUser() - .setUserAttribute('color', 'blue'); - forwarderInstance.receivedEvent = null; - mParticle.logEvent('test event', mParticle.EventType.Other); - const eventResult = forwarderInstance.receivedEvent; - expect(eventResult).to.be.ok; - expect(eventResult.UserAttributes).to.be.ok; - expect(eventResult.UserAttributes).to.not.have.property( - 'secret' - ); - expect(eventResult.UserAttributes).to.have.property( - 'color', - 'blue' - ); - - // --- Batch path: 'secret' attribute should be stripped --- - let receivedBatch: any = null; - forwarderInstance.processBatch = function(batch: any) { - receivedBatch = batch; - }; - - const fakeBatch = { - events: [ - { - event_type: 'custom_event', - data: { - event_name: 'test event', - custom_event_type: 'other', - }, - }, - ], - mpid: testMPID, - user_attributes: { secret: 'hidden', color: 'blue' }, - }; - - mParticle - .getInstance() - ._Forwarders.sendBatchToForwarders(fakeBatch); - - expect(receivedBatch).to.be.ok; - expect(receivedBatch.user_attributes).to.not.have.property( - 'secret' - ); - expect(receivedBatch.user_attributes).to.have.property( - 'color', - 'blue' - ); - }); - }); }); \ No newline at end of file