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 Feb 29, 2024
1 parent fbddd45 commit 75cf227
Show file tree
Hide file tree
Showing 2 changed files with 370 additions and 0 deletions.
185 changes: 185 additions & 0 deletions dist/GoogleAdWordsEventForwarder.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,39 @@ Object.defineProperty(exports, '__esModule', { value: true });
},
ENHANCED_CONVERSION_DATA = "GoogleAds.ECData";

// Declares valid Google consent values
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',
];

var constructor = function () {
var self = this,
isInitialized = false,
forwarderSettings,
labels,
customAttributeMappings,
consentMappings = [],
consentPayloadDefaults = {},
consentPayloadAsString = '',
reportingService,
eventQueue = [],
gtagSiteId;
Expand All @@ -55,6 +82,28 @@ Object.defineProperty(exports, '__esModule', { value: true });

try {
if (window.gtag && forwarderSettings.enableGtag == 'True') {
// If consent payload is empty,
// we never sent an initial default consent state
// so we shouldn't send an update.
if (consentPayloadAsString && forwarderSettings.consentMappingWeb) {
var eventConsentState = getEventConsentState(event.ConsentState);

if (!isEmpty(eventConsentState)) {
var updatedConsentPayload = generateConsentStatePayloadFromMappings(
eventConsentState,
consentMappings,
);

var eventConsentAsString =
JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== consentPayloadAsString) {
sendGtagConsentUpdate(updatedConsentPayload);
consentPayloadAsString = eventConsentAsString;
}
}
}

if (
forwarderSettings.enableEnhancedConversions ===
'True' &&
Expand Down Expand Up @@ -168,7 +217,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
}
}

function getUserConsentState() {
var userConsentState = {};

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

if (!currentUser) {
return {};
}

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

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

return userConsentState;
}

function getEventConsentState(eventConsentState) {
return eventConsentState && eventConsentState.getGDPRConsentState
? eventConsentState.getGDPRConsentState()
: {};
}
// ** Adwords Events
function getBaseAdWordEvent() {
var adWordEvent = {};
Expand Down Expand Up @@ -210,6 +284,31 @@ Object.defineProperty(exports, '__esModule', { value: true });
return adWordEvent;
}

function getConsentSettings() {
var consentSettings = {};

var googleToMpConsentSettingsMapping = {
// Inherited from S2S Integration Settings
ad_user_data: 'defaultAdUserDataConsent',
ad_personalization: 'defaultAdPersonalizationConsent',

// Unique to Web Kits
ad_storage: 'defaultAdStorageConsentWeb',
analytics_storage: 'defaultAnalyticsStorageConsentWeb'
};

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

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

return consentSettings;
}

// gtag Events
function getBaseGtagEvent(conversionLabel) {
return {
Expand Down Expand Up @@ -250,6 +349,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
}

function sendGtagEvent(payload) {
// https://go.mparticle.com/work/SQDSDKS-6165
try {
gtag('event', 'conversion', payload);
} catch (e) {
Expand All @@ -259,6 +359,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
return true;
}

function sendGtagConsentDefaults(payload) {
// https://go.mparticle.com/work/SQDSDKS-6165
try {
gtag('consent', 'default', payload);
} catch (e) {
console.error('gtag is not available to send consent defaults: ', payload, e);
return false;
}
return true;
}

function sendGtagConsentUpdate(payload) {
// https://go.mparticle.com/work/SQDSDKS-6165
try {
gtag('consent', 'update', payload);
} catch (e) {
console.error('gtag is not available to send consent update: ', payload, e);
return false;
}
return true;
}

function sendAdwordsEvent(payload) {
try {
window.google_trackConversion(payload);
Expand All @@ -269,6 +391,30 @@ Object.defineProperty(exports, '__esModule', { value: true });
return true;
}

// Creates a new Consent State Payload based on Consent State and Mapping
function generateConsentStatePayloadFromMappings(consentState, mappings) {
if (!mappings) return {};
var payload = cloneObject(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;
}

// Looks up an Event's conversionLabel from customAttributeMappings based on computed jsHash value
function getConversionLabel(event) {
var jsHash = calculateJSHash(event.EventDataType, event.EventCategory, event.EventName);
Expand Down Expand Up @@ -366,6 +512,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
})();
}

// https://go.mparticle.com/work/SQDSDKS-6166
function initForwarder(settings, service, testMode) {
window.enhanced_conversion_data = {};
forwarderSettings = settings;
Expand Down Expand Up @@ -393,6 +540,36 @@ Object.defineProperty(exports, '__esModule', { value: true });
return 'Can\'t initialize forwarder: ' + name + ', conversionId is not defined';
}

// https://go.mparticle.com/work/SQDSDKS-6165
if (window.gtag && forwarderSettings.enableGtag === 'True') {
if (forwarderSettings.consentMappingWeb) {
consentMappings = parseSettingsString(
forwarderSettings.consentMappingWeb
);
} else {
// Ensures consent mappings is an empty array
// for future use
consentMappings = [];
}

consentPayloadDefaults = getConsentSettings();
var initialConsentState = getUserConsentState();

var defaultConsentPayload =
generateConsentStatePayloadFromMappings(
initialConsentState,
consentMappings
);

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

sendGtagConsentDefaults(defaultConsentPayload);
}
}

forwarderSettings.remarketingOnly = forwarderSettings.remarketingOnly == 'True';

try {
Expand Down Expand Up @@ -462,10 +639,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
console.log('Successfully registered ' + name + ' to your mParticle configuration');
}

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

function isObject(val) {
return val != null && typeof val === 'object' && Array.isArray(val) === false;
}

function isEmpty(value) {
return value == null || !(Object.keys(value) || value).length;
}

function mergeObjects() {
var resObj = {};
for(var i=0; i < arguments.length; i += 1) {
Expand Down
Loading

0 comments on commit 75cf227

Please sign in to comment.