Skip to content

Commit

Permalink
chore(build): Generate latest bundle [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
mparticle-automation committed Mar 1, 2024
1 parent 388c346 commit b6be2e0
Show file tree
Hide file tree
Showing 2 changed files with 382 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,123 @@

Object.defineProperty(exports, '__esModule', { value: true });

var googleConsentValues = {
// Server Integration uses 'Unspecified' as a value when the setting is 'not set'.
// However, this is not used by Google's Web SDK. We are referencing it here as a comment
// as a record of this distinction and for posterity.
// If Google ever adds this for web, the line can just be uncommented to support this.
//
// Docs:
// Web: https://developers.google.com/tag-platform/gtagjs/reference#consent
// S2S: https://developers.google.com/google-ads/api/reference/rpc/v15/ConsentStatusEnum.ConsentStatus
//
// Unspecified: 'unspecified',
Denied: 'denied',
Granted: 'granted',
};

// Declares list of valid Google Consent Properties
var googleConsentProperties = [
'ad_storage',
'ad_user_data',
'ad_personalization',
'analytics_storage',
];

function ConsentHandler(common) {
this.common = common || {};
}

ConsentHandler.prototype.getUserConsentState = function () {
var userConsentState = {};

if (mParticle.Identity && mParticle.Identity.getCurrentUser) {
var currentUser = mParticle.Identity.getCurrentUser();

if (!currentUser) {
return {};
}

var consentState =
mParticle.Identity.getCurrentUser().getConsentState();

if (consentState && consentState.getGDPRConsentState) {
userConsentState = consentState.getGDPRConsentState();
}
}

return userConsentState;
};

ConsentHandler.prototype.getEventConsentState = function (eventConsentState) {
return eventConsentState && eventConsentState.getGDPRConsentState
? eventConsentState.getGDPRConsentState()
: {};
};

ConsentHandler.prototype.getConsentSettings = function () {
var consentSettings = {};

var googleToMpConsentSettingsMapping = {
ad_user_data: 'defaultAdUserDataConsentSDK',
ad_personalization: 'defaultAdPersonalizationConsentSDK',
ad_storage: 'defaultAdStorageConsentSDK',
analytics_storage: 'defaultAnalyticsStorageConsentSDK',
};

var forwarderSettings = this.common.forwarderSettings;

Object.keys(googleToMpConsentSettingsMapping).forEach(function (
googleConsentKey
) {
var mpConsentSettingKey =
googleToMpConsentSettingsMapping[googleConsentKey];
var googleConsentValuesKey = forwarderSettings[mpConsentSettingKey];

if (googleConsentValuesKey && mpConsentSettingKey) {
consentSettings[googleConsentKey] =
googleConsentValues[googleConsentValuesKey];
}
});

return consentSettings;
};

ConsentHandler.prototype.generateConsentStatePayloadFromMappings = function (
consentState,
mappings
) {
if (!mappings) return {};

var payload = this.common.cloneObject(this.common.consentPayloadDefaults);

for (var i = 0; i <= mappings.length - 1; i++) {
var mappingEntry = mappings[i];
var mpMappedConsentName = mappingEntry.map;
var googleMappedConsentName = mappingEntry.value;

if (
consentState[mpMappedConsentName] &&
googleConsentProperties.indexOf(googleMappedConsentName) !== -1
) {
payload[googleMappedConsentName] = consentState[mpMappedConsentName]
.Consented
? googleConsentValues.Granted
: googleConsentValues.Denied;
}
}

return payload;
};

var consent = ConsentHandler;

// Google requires event and user attribute strings to have specific limits
// in place when sending to data layer.
// https://support.google.com/analytics/answer/11202874?sjid=7958830619827381593-NA



var EVENT_NAME_MAX_LENGTH = 40;
var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40;
var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100;
Expand Down Expand Up @@ -37,7 +150,13 @@ function isEmpty(value) {
return value == null || !(Object.keys(value) || value).length;
}

function Common() {}
function Common() {
this.consentMappings = [];
this.consentPayloadDefaults = {};
this.consentPayloadAsString = '';

this.consentHandler = new consent(this);
}

Common.prototype.forwarderSettings = null;

Expand Down Expand Up @@ -309,6 +428,12 @@ Common.prototype.getUserId = function (
}
};

Common.prototype.cloneObject = function (obj) {
return JSON.parse(JSON.stringify(obj));
};

Common.prototype.isEmpty = isEmpty;

var common = Common;

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
Expand Down Expand Up @@ -832,6 +957,32 @@ function EventHandler(common) {
this.common = common || {};
}

EventHandler.prototype.maybeSendConsentUpdateToGa4 = function (event) {
// If consent payload is empty,
// we never sent an initial default consent state
// so we shouldn't send an update.
if (this.common.consentPayloadAsString && this.common.consentMappings) {
var eventConsentState = this.common.consentHandler.getEventConsentState(
event.ConsentState
);

if (!this.common.isEmpty(eventConsentState)) {
var updatedConsentPayload =
this.common.consentHandler.generateConsentStatePayloadFromMappings(
eventConsentState,
this.common.consentMappings
);

var eventConsentAsString = JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== this.common.consentPayloadAsString) {
gtag('consent', 'update', updatedConsentPayload);
this.common.consentPayloadAsString = eventConsentAsString;
}
}
}
};

// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
var standardizedEventName;
Expand All @@ -857,6 +1008,7 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
};

EventHandler.prototype.logEvent = function (event) {
this.maybeSendConsentUpdateToGa4(event);
this.sendEventToGA4(event.EventName, event.EventAttributes);
};

Expand Down Expand Up @@ -897,6 +1049,7 @@ EventHandler.prototype.logPageView = function (event) {
event.EventAttributes
);

this.maybeSendConsentUpdateToGa4(event);
this.sendEventToGA4('page_view', eventAttributes);

return true;
Expand Down Expand Up @@ -996,7 +1149,7 @@ var initialization = {
// 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
// However, when initialized via npm, it does not exist, and so we have
// to set it manually here.
window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {};

Expand All @@ -1010,6 +1163,19 @@ var initialization = {
var configSettings = {
send_page_view: forwarderSettings.enablePageView === 'True',
};

if (forwarderSettings.consentMappingSDK) {
common.consentMappings = parseSettingsString(
forwarderSettings.consentMappingSDK
);
} else {
// Ensures consent mappings is an empty array
// for future use
common.consentMappings = [];
common.consentPayloadDefaults = {};
common.consentPayloadAsString = '';
}

window.dataLayer = window.dataLayer || [];

window.gtag = function () {
Expand Down Expand Up @@ -1059,6 +1225,25 @@ var initialization = {
} else {
isInitialized = true;
}

common.consentPayloadDefaults =
common.consentHandler.getConsentSettings();
var initialConsentState = common.consentHandler.getUserConsentState();

var defaultConsentPayload =
common.consentHandler.generateConsentStatePayloadFromMappings(
initialConsentState,
common.consentMappings
);

if (!common.isEmpty(defaultConsentPayload)) {
common.consentPayloadAsString = JSON.stringify(
defaultConsentPayload
);

gtag('consent', 'default', defaultConsentPayload);
}

return isInitialized;
},
};
Expand All @@ -1074,6 +1259,10 @@ function setClientId(clientId, moduleId) {
window.mParticle._setIntegrationDelay(moduleId, false);
}

function parseSettingsString(settingsString) {
return JSON.parse(settingsString.replace(/&quot;/g, '"'));
}

var initialization_1 = initialization;

var sessionHandler = {
Expand Down
Loading

0 comments on commit b6be2e0

Please sign in to comment.