Skip to content

Commit

Permalink
feat: Add Customer Configurable Standardization (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 authored Sep 26, 2023
1 parent 52121cb commit 712f23a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/GA4Client/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion packages/GA4Client/src/initialization.js
Original file line number Diff line number Diff line change
@@ -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 ******
Expand All @@ -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';
Expand All @@ -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 || [];

Expand Down
32 changes: 32 additions & 0 deletions packages/GA4Client/test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 712f23a

Please sign in to comment.