From f2b8bbdae9002ce2a03d34c77f5c443664bfbca3 Mon Sep 17 00:00:00 2001 From: Utsab Chowdhury Date: Mon, 10 Jun 2024 16:35:18 +0530 Subject: [PATCH] chore: onboard ga4 v2 --- src/v0/destinations/ga4/transform.js | 5 --- .../{ga4 => ga4_v2}/customMappingsHandler.js | 4 +-- src/v0/destinations/ga4_v2/transform.ts | 34 +++++++++++++++++++ .../destinations/ga4/processor/data.ts | 3 +- .../integrations/destinations/ga4_v2/mocks.ts | 5 +++ .../processor/customMappings.ts | 11 +++--- .../destinations/ga4_v2/processor/data.ts | 3 ++ 7 files changed, 51 insertions(+), 14 deletions(-) rename src/v0/destinations/{ga4 => ga4_v2}/customMappingsHandler.js (99%) create mode 100644 src/v0/destinations/ga4_v2/transform.ts create mode 100644 test/integrations/destinations/ga4_v2/mocks.ts rename test/integrations/destinations/{ga4 => ga4_v2}/processor/customMappings.ts (99%) create mode 100644 test/integrations/destinations/ga4_v2/processor/data.ts diff --git a/src/v0/destinations/ga4/transform.js b/src/v0/destinations/ga4/transform.js index 9e7b2164b3..9de33bcc5c 100644 --- a/src/v0/destinations/ga4/transform.js +++ b/src/v0/destinations/ga4/transform.js @@ -35,7 +35,6 @@ const { buildDeliverablePayload, } = require('./utils'); require('../../util/constant'); -const { handleCustomMappings } = require('./customMappingsHandler'); /** * Returns response for GA4 destination @@ -224,10 +223,6 @@ const process = (event) => { const messageType = message.type.toLowerCase(); - if (Array.isArray(Config.eventsMapping) && Config.eventsMapping.length > 0) { - // custom mappings flow - return handleCustomMappings(message, Config); - } let response; switch (messageType) { case EventType.TRACK: diff --git a/src/v0/destinations/ga4/customMappingsHandler.js b/src/v0/destinations/ga4_v2/customMappingsHandler.js similarity index 99% rename from src/v0/destinations/ga4/customMappingsHandler.js rename to src/v0/destinations/ga4_v2/customMappingsHandler.js index 4750f83bf3..5e16660256 100644 --- a/src/v0/destinations/ga4/customMappingsHandler.js +++ b/src/v0/destinations/ga4_v2/customMappingsHandler.js @@ -13,7 +13,7 @@ const { GA4_PARAMETERS_EXCLUSION, prepareUserProperties, sanitizeUserProperties, -} = require('./utils'); +} = require('../ga4/utils'); const { InstrumentationError } = require('@rudderstack/integrations-lib'); const { removeUndefinedAndNullRecurse, @@ -25,7 +25,7 @@ const { getIntegrationsObj, applyCustomMappings, } = require('../../util'); -const { trackCommonConfig, ConfigCategory, mappingConfig } = require('./config'); +const { trackCommonConfig, ConfigCategory, mappingConfig } = require('../ga4/config'); const findGA4Events = (eventsMapping, event) => { // Find the event using destructuring and early return diff --git a/src/v0/destinations/ga4_v2/transform.ts b/src/v0/destinations/ga4_v2/transform.ts new file mode 100644 index 0000000000..2c54a058c2 --- /dev/null +++ b/src/v0/destinations/ga4_v2/transform.ts @@ -0,0 +1,34 @@ +import { + ConfigurationError, + InstrumentationError, + RudderStackEvent, +} from '@rudderstack/integrations-lib'; +import { ProcessorTransformationRequest } from '../../../types'; +import { handleCustomMappings } from './customMappingsHandler'; + +export function process(event: ProcessorTransformationRequest) { + const { message, destination } = event; + const { Config } = destination; + + if (!Config.typesOfClient) { + throw new ConfigurationError('Client type not found. Aborting '); + } + if (!Config.apiSecret) { + throw new ConfigurationError('API Secret not found. Aborting '); + } + if (Config.typesOfClient === 'gtag' && !Config.measurementId) { + throw new ConfigurationError('measurementId must be provided. Aborting'); + } + if (Config.typesOfClient === 'firebase' && !Config.firebaseAppId) { + throw new ConfigurationError('firebaseAppId must be provided. Aborting'); + } + + const eventPayload = message as RudderStackEvent; + + if (!eventPayload.type) { + throw new InstrumentationError('Message Type is not present. Aborting message.'); + } + + // custom mappings flow + return handleCustomMappings(message, Config); +} diff --git a/test/integrations/destinations/ga4/processor/data.ts b/test/integrations/destinations/ga4/processor/data.ts index 5c374d282c..fb65787214 100644 --- a/test/integrations/destinations/ga4/processor/data.ts +++ b/test/integrations/destinations/ga4/processor/data.ts @@ -1,4 +1,3 @@ -import { customMappingTestCases } from './customMappings'; import { existingTests } from './exisitngTests'; -export const data = [...existingTests, ...customMappingTestCases]; +export const data = [...existingTests]; diff --git a/test/integrations/destinations/ga4_v2/mocks.ts b/test/integrations/destinations/ga4_v2/mocks.ts new file mode 100644 index 0000000000..3a27349ff7 --- /dev/null +++ b/test/integrations/destinations/ga4_v2/mocks.ts @@ -0,0 +1,5 @@ +export const defaultMockFns = () => { + return jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2022-04-29T05:17:09Z').valueOf()); +}; diff --git a/test/integrations/destinations/ga4/processor/customMappings.ts b/test/integrations/destinations/ga4_v2/processor/customMappings.ts similarity index 99% rename from test/integrations/destinations/ga4/processor/customMappings.ts rename to test/integrations/destinations/ga4_v2/processor/customMappings.ts index 04cec70963..79afd83329 100644 --- a/test/integrations/destinations/ga4/processor/customMappings.ts +++ b/test/integrations/destinations/ga4_v2/processor/customMappings.ts @@ -84,7 +84,7 @@ const eventsMapping = [ }, { to: '$.events[0].params.items[*].name', - from: "$.properties.products[?(@.name=='Salt')].name", + from: '$.properties.products[*].name', }, { to: '$.events[0].params.prices', @@ -227,7 +227,7 @@ const destination = { }; export const customMappingTestCases = [ { - name: 'ga4', + name: 'ga4_v2', id: 'ga4_custom_mapping_test_0', description: 'Custom Mapping Test 0', feature: 'processor', @@ -293,6 +293,7 @@ export const customMappingTestCases = [ id: 213123, key: 'someValue', list_id: 'random_list_id', + name: 'Sugar', }, ], prices: 456, @@ -327,7 +328,7 @@ export const customMappingTestCases = [ mockFns: defaultMockFns, }, { - name: 'ga4', + name: 'ga4_v2', id: 'ga4_custom_mapping_test_1', description: 'Custom Mapping Test for multiplexing', feature: 'processor', @@ -491,7 +492,7 @@ export const customMappingTestCases = [ mockFns: defaultMockFns, }, { - name: 'ga4', + name: 'ga4_v2', id: 'ga4_custom_mapping_test_2', description: 'Custom Mapping Test For mapping not present in events mapping', feature: 'processor', @@ -616,7 +617,7 @@ export const customMappingTestCases = [ mockFns: defaultMockFns, }, { - name: 'ga4', + name: 'ga4_v2', id: 'ga4_custom_mapping_test_3', description: 'Custom Mapping Test For Group Event Type', feature: 'processor', diff --git a/test/integrations/destinations/ga4_v2/processor/data.ts b/test/integrations/destinations/ga4_v2/processor/data.ts new file mode 100644 index 0000000000..ba82792f31 --- /dev/null +++ b/test/integrations/destinations/ga4_v2/processor/data.ts @@ -0,0 +1,3 @@ +import { customMappingTestCases } from './customMappings'; + +export const data = [...customMappingTestCases];