diff --git a/packages/GA4Client/src/common.js b/packages/GA4Client/src/common.js index c99ff76..bf8ff43 100644 --- a/packages/GA4Client/src/common.js +++ b/packages/GA4Client/src/common.js @@ -132,6 +132,15 @@ Common.prototype.standardizeParameters = function (parameters) { }; Common.prototype.standardizeName = function (name) { + try { + name = window.GoogleAnalytics4Kit.setCustomNameStandardization(name); + } catch (e) { + console.error( + 'Error calling setCustomNameStandardization callback. Check your callback. Data will still be sent without user-defined standardization. See our docs for proper use - https://docs.mparticle.com/integrations/google-analytics-4/event/', + e + ); + } + // names of events and parameters have the following requirements: // 1. They must only contain letters, numbers, and underscores function removeForbiddenCharacters(name) { diff --git a/packages/GA4Client/src/initialization.js b/packages/GA4Client/src/initialization.js index ac478de..0b764e8 100644 --- a/packages/GA4Client/src/initialization.js +++ b/packages/GA4Client/src/initialization.js @@ -1,4 +1,6 @@ var initialization = { + // This name matches the mParticle database. This should not be changed unless the database name is being changed + // Changing this will also break the API for the cleansing data callback that a customer uses. name: 'GoogleAnalytics4', moduleId: 160, /* ****** Fill out initForwarder to load your SDK ****** @@ -21,6 +23,13 @@ var initialization = { ) { mParticle._setIntegrationDelay(this.moduleId, true); + // The API to allow a customer to provide the cleansing callback + // relies on window.GoogleAnalytics4Kit to exist. When MP is initialized + // via the snippet, the kit code adds it to the window automatically. + // However, when initialized via npm, it does not exist, and so we have + // to set it manually here. + window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {}; + common.forwarderSettings = forwarderSettings; common.forwarderSettings.enableDataCleansing = common.forwarderSettings.enableDataCleansing === 'True'; @@ -29,7 +38,7 @@ var initialization = { var hashUserId = forwarderSettings.hashUserId; var configSettings = { - send_page_view: forwarderSettings.enablePageView === 'True' + send_page_view: forwarderSettings.enablePageView === 'True', }; window.dataLayer = window.dataLayer || []; diff --git a/packages/GA4Client/test/src/tests.js b/packages/GA4Client/test/src/tests.js index 758d82d..dc87cda 100644 --- a/packages/GA4Client/test/src/tests.js +++ b/packages/GA4Client/test/src/tests.js @@ -2021,6 +2021,38 @@ describe('Google Analytics 4 Event', function () { done(); }); + it('should standardize event names and attributes keys using user provided cleansing callback first, and then our standardization', function (done) { + window.GoogleAnalytics4Kit.setCustomNameStandardization = + function (str) { + return str.slice(0, str.length - 1); + }; + + mParticle.forwarder.process({ + EventDataType: MessageType.PageEvent, + EventCategory: EventType.Navigation, + EventName: '2?Test Event ?Standardization', + EventAttributes: { + foo: 'bar', + '1?test4ever!!!': 'tester', + }, + }); + + var expectedEventName = 'Test_Event__Standardizatio'; + + var expectedEventAttributes = { + fo: 'bar', + test4ever__: 'tester', + }; + + window.dataLayer[0][1].should.eql(expectedEventName); + window.dataLayer[0][2].should.eql(expectedEventAttributes); + + // remove this function so that it doesn't affect other tests + delete window.GoogleAnalytics4Kit.setCustomNameStandardization; + + done(); + }); + it('should remove forbidden prefixes', function (done) { mParticle.forwarder.process({ EventDataType: MessageType.PageEvent,