Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Customer Configurable Standardization #53

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading