Skip to content

Commit c851914

Browse files
committed
feat: Add Customer Configurable Standardization (#53)
1 parent 52121cb commit c851914

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

packages/GA4Client/src/common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ Common.prototype.standardizeParameters = function (parameters) {
132132
};
133133

134134
Common.prototype.standardizeName = function (name) {
135+
try {
136+
name = window.GoogleAnalytics4Kit.setCustomNameStandardization(name);
137+
} catch (e) {
138+
console.error(
139+
'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/',
140+
e
141+
);
142+
}
143+
135144
// names of events and parameters have the following requirements:
136145
// 1. They must only contain letters, numbers, and underscores
137146
function removeForbiddenCharacters(name) {

packages/GA4Client/src/initialization.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var initialization = {
2+
// This name matches the mParticle database. This should not be changed unless the database name is being changed
3+
// Changing this will also break the API for the cleansing data callback that a customer uses.
24
name: 'GoogleAnalytics4',
35
moduleId: 160,
46
/* ****** Fill out initForwarder to load your SDK ******
@@ -21,6 +23,13 @@ var initialization = {
2123
) {
2224
mParticle._setIntegrationDelay(this.moduleId, true);
2325

26+
// The API to allow a customer to provide the cleansing callback
27+
// relies on window.GoogleAnalytics4Kit to exist. When MP is initialized
28+
// via the snippet, the kit code adds it to the window automatically.
29+
// However, when initialized via npm, it does not exist, and so we have
30+
// to set it manually here.
31+
window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {};
32+
2433
common.forwarderSettings = forwarderSettings;
2534
common.forwarderSettings.enableDataCleansing =
2635
common.forwarderSettings.enableDataCleansing === 'True';
@@ -29,7 +38,7 @@ var initialization = {
2938
var hashUserId = forwarderSettings.hashUserId;
3039

3140
var configSettings = {
32-
send_page_view: forwarderSettings.enablePageView === 'True'
41+
send_page_view: forwarderSettings.enablePageView === 'True',
3342
};
3443
window.dataLayer = window.dataLayer || [];
3544

packages/GA4Client/test/src/tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,38 @@ describe('Google Analytics 4 Event', function () {
20212021
done();
20222022
});
20232023

2024+
it('should standardize event names and attributes keys using user provided cleansing callback first, and then our standardization', function (done) {
2025+
window.GoogleAnalytics4Kit.setCustomNameStandardization =
2026+
function (str) {
2027+
return str.slice(0, str.length - 1);
2028+
};
2029+
2030+
mParticle.forwarder.process({
2031+
EventDataType: MessageType.PageEvent,
2032+
EventCategory: EventType.Navigation,
2033+
EventName: '2?Test Event ?Standardization',
2034+
EventAttributes: {
2035+
foo: 'bar',
2036+
'1?test4ever!!!': 'tester',
2037+
},
2038+
});
2039+
2040+
var expectedEventName = 'Test_Event__Standardizatio';
2041+
2042+
var expectedEventAttributes = {
2043+
fo: 'bar',
2044+
test4ever__: 'tester',
2045+
};
2046+
2047+
window.dataLayer[0][1].should.eql(expectedEventName);
2048+
window.dataLayer[0][2].should.eql(expectedEventAttributes);
2049+
2050+
// remove this function so that it doesn't affect other tests
2051+
delete window.GoogleAnalytics4Kit.setCustomNameStandardization;
2052+
2053+
done();
2054+
});
2055+
20242056
it('should remove forbidden prefixes', function (done) {
20252057
mParticle.forwarder.process({
20262058
EventDataType: MessageType.PageEvent,

0 commit comments

Comments
 (0)