Skip to content

Commit

Permalink
[update] - GA4 Event Tag Update
Browse files Browse the repository at this point in the history
Change-Id: I2d9135bc29e320df72866c39176cbd39709e587a
  • Loading branch information
b-kuehn committed Nov 28, 2023
1 parent 1c47633 commit 9a4d06d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 62 deletions.
90 changes: 53 additions & 37 deletions features/eventTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022 Google LLC
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,9 @@ function writeGA4EventTagstoSheet() {
const workspacePath = getSelectedWorkspacePath();
const workspaceName = getSelectedWorkspaceName();
const tags = listGTMResources('tags', workspacePath);
const variables = listGTMResources('variables', workspacePath);
const triggers = listGTMResources('triggers', workspacePath);
writeGTMVariablesToValidationSheet();
triggers.push({
triggerId: '2147479553',
name: 'All Pages'
Expand All @@ -34,15 +36,15 @@ function writeGA4EventTagstoSheet() {
});
// Retrieve the GA4 event tags.
const ga4EventTags = listGA4EventTags(tags);
clearRangeContent('validation', 'config tag names');
writeConfigTagNamesToValidationSheet(tags);
clearRangeContent('validation', 'event setting variable names');
writeSettingVariableNamesToValidationSheet(variables);
clearRangeContent('validation', 'tag names');
writeTagNamesToValidationSheet(tags);
// Add specific event tag information to a double array to be written to the
// sheet.
const formattedGA4EventTags = ga4EventTags.reduce((arr, tag) => {
const tempArray = [];
while (tempArray.length < 23) {
while (tempArray.length < 24) {
tempArray.push('');
}
const sendEcommerceData = tag.parameter.find(
Expand All @@ -69,37 +71,43 @@ function writeGA4EventTagstoSheet() {
}
}
tempArray[8] = tag.parameter.find(
param => param.key == 'measurementId').value;
param => param.key == 'measurementIdOverride').value;
const eventSettingsVariable = tag.parameter.find(
param => param.key == 'eventSettingsVariable');
tempArray[9] = '';
if (eventSettingsVariable) {
tempArray[9] = eventSettingsVariable.value;
}
if (tag.priority != undefined) {
tempArray[9] = tag.priority.value;
tempArray[10] = tag.priority.value;
}
tempArray[10] = tag.scheduleStartMs;
tempArray[11] = tag.scheduleEndMs;
tempArray[12] = tag.liveOnly;
tempArray[11] = tag.scheduleStartMs;
tempArray[12] = tag.scheduleEndMs;
tempArray[13] = tag.liveOnly;
if (tag.setupTag != undefined) {
tempArray[13] = tag.setupTag[0].tagName;
tempArray[14] = tag.setupTag[0].stopOnSetupFailure;
tempArray[14] = tag.setupTag[0].tagName;
tempArray[15] = tag.setupTag[0].stopOnSetupFailure;
}
if (tag.teardownTag != undefined) {
tempArray[15] = tag.teardownTag[0].tagName;
tempArray[16] = tag.teardownTag[0].stopTeardownOnFailure;
tempArray[16] = tag.teardownTag[0].tagName;
tempArray[17] = tag.teardownTag[0].stopTeardownOnFailure;
}
tempArray[17] = tag.tagFiringOption;
tempArray[18] = tag.consentSettings.consentStatus;
tempArray[18] = tag.tagFiringOption;
tempArray[19] = tag.consentSettings.consentStatus;
if (tag.consentSettings.consentStatus == 'needed') {
tempArray[19] = tag.consentSettings.consentType.list
tempArray[20] = tag.consentSettings.consentType.list
.reduce((arr, consentType) => {
arr.push(consentType.value);
return arr;
}, []).join(', ');
}
tempArray[20] = tag.monitoringMetadataTagNameKey;
tempArray[21] = tag.monitoringMetadataTagNameKey;
if (tag.firingTriggerId != undefined) {
tempArray[21] = convertTriggerIdsToNames(
tempArray[22] = convertTriggerIdsToNames(
tag.firingTriggerId, triggers).join(', ');
}
if (tag.blockingTriggerId != undefined) {
tempArray[22] = convertTriggerIdsToNames(
tempArray[23] = convertTriggerIdsToNames(
tag.blockingTriggerId, triggers).join(', ');
}
arr.push(tempArray);
Expand Down Expand Up @@ -181,21 +189,22 @@ function buildGA4EventPayload(settings, triggers, tags, modifyOption) {
const eventName = settings[5];
const sendEcommData = Boolean(settings[6]);
const ecommObject = settings[7];
const configData = settings[8];
const tagFiringPriority = settings[9];
const scheduleStart = settings[10];
const scheduleEnd = settings[11];
const onlyFireInPublished = Boolean(settings[12]);
const setupTag = settings[13];
const stopOnSetupFailure = Boolean(settings[14]);
const teardownTag = settings[15];
const stopTeardownOnFailure = Boolean(settings[16]);
const tagFiringOption = settings[17];
const consentStatus = settings[18];
const consentStatusType = settings[19];
const metadataTagName = settings[20];
const firingTriggerNames = settings[21];
const blockingTriggerNames = settings[22];
const measurementIdOverride = settings[8].trim();
const eventSettingsVariable = settings[9];
const tagFiringPriority = settings[10];
const scheduleStart = settings[11];
const scheduleEnd = settings[12];
const onlyFireInPublished = Boolean(settings[13]);
const setupTag = settings[14];
const stopOnSetupFailure = Boolean(settings[15]);
const teardownTag = settings[16];
const stopTeardownOnFailure = Boolean(settings[17]);
const tagFiringOption = settings[18];
const consentStatus = settings[19];
const consentStatusType = settings[20];
const metadataTagName = settings[21];
const firingTriggerNames = settings[22];
const blockingTriggerNames = settings[23];
const firingTriggerIds = convertTriggerNamesToIds(
firingTriggerNames, triggers);
const blockingTriggerIds = convertTriggerNamesToIds(
Expand All @@ -218,11 +227,18 @@ function buildGA4EventPayload(settings, triggers, tags, modifyOption) {
key: 'eventName',
value: eventName
}, {
type: 'tagReference',
key: 'measurementId',
value: configData
type: 'template',
key: 'measurementIdOverride',
value: measurementIdOverride
}]
}
if (eventSettingsVariable.length > 0) {
newTag.parameter.push({
"type": "template",
"key": "eventSettingsVariable",
"value": eventSettingsVariable
});
}
if (setupTag.length > 0) {
newTag.setupTag = [{
tagName: setupTag,
Expand Down
14 changes: 10 additions & 4 deletions features/paramsAndUserProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
function writeExistingParamsAndUserPropsToSheet() {
clearRangeContent('modifyParamsAndUserProps', 'settings');
writeGTMVariablesToValidationSheet();
writeToSheet(
listParamsAndUserProperties(), 'modifyParamsAndUserProps', 'settings');
}
Expand Down Expand Up @@ -52,14 +53,15 @@ function writeGA4TagsToParamsAndUserPropertiesSheet() {
*/
function listParamsAndUserProperties() {
const tags = listGTMResources('tags', getSelectedWorkspacePath());
writeGTMVariablesToValidationSheet();
const ga4EventTags = listGA4EventTags(tags);
return ga4EventTags.reduce((arr, eventTag) => {
const eventName = eventTag.parameter.find(
param => param.key == 'eventName').value;
eventTag.parameter.forEach(param => {
if (param.type == 'list') {
let mapType = '';
if (param.key == 'eventParameters') {
if (param.key == 'eventSettingsTable') {
mapType = 'parameter';
} else if (param.key == 'userProperties') {
mapType = 'user_property';
Expand Down Expand Up @@ -106,7 +108,7 @@ function modifyParametersAndUserProperties() {
let userPropertiesAdded = false;
tag.parameter.forEach(param => {
if (param.type == 'list') {
if (param.key == 'eventParameters') {
if (param.key == 'eventSettingsTable') {
param.list = removeParams(
param.list, paramAndUserPropertySettings.remove.parameter);
param.list = param.list.concat(
Expand All @@ -125,7 +127,7 @@ function modifyParametersAndUserProperties() {
paramAndUserPropertySettings.create.parameter.length > 0) {
tag.parameter.push({
type: 'list',
key: 'eventParameters',
key: 'eventSettingsTable',
list: paramAndUserPropertySettings.create.parameter
});
}
Expand Down Expand Up @@ -180,7 +182,11 @@ function organizeParamsAndUserPropsByTag() {
};
}
if (action == 'Create') {
tagObj[tagId].create[type].push(buildMapObject(name, value));
if (type == 'parameter') {
tagObj[tagId].create[type].push(buildParameterMapObject(name, value));
} else if (type == 'user_property') {
tagObj[tagId].create[type].push(buildUserPropertyMapObject(name, value));
}
} else if (action == 'Delete') {
tagObj[tagId].remove[type].push(
{name: name.toString(), value:value.toString()});
Expand Down
8 changes: 4 additions & 4 deletions shared/rangeData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022 Google LLC
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -110,7 +110,7 @@ const sheetsMeta = {
numColumns: 1
}
}, {
name: 'config tag names',
name: 'event setting variable names',
read: {
row: 2,
column: 3,
Expand All @@ -133,13 +133,13 @@ const sheetsMeta = {
row: 2,
column: 1,
numRows: 1,
numColumns: 26
numColumns: 27
},
write: {
row: 2,
column: 1,
numRows: 1,
numColumns: 23
numColumns: 24
}
}]
},
Expand Down
49 changes: 32 additions & 17 deletions shared/shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022 Google LLC
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,7 +79,6 @@ function writeToSheet(data, sheetName, rangeName) {
if (data.length > 0) {
sheet.getRange(ranges.row, ranges.column, data.length, ranges.numColumns).setValues(data);
}
checkRelease();
}

/**
Expand Down Expand Up @@ -115,14 +114,30 @@ function clearRangeContent(sheetsMetaField, rangeName) {
* @param {string} value The value of the entity being mapped.
* @return {!Object} The map object that can be added to a tag or variable.
*/
function buildMapObject (name, value) {
return {
map: [
{value: name, type: 'template', key: 'name'},
{value: value.toString(), type: 'template', key: 'value'}
],
type: 'map'
};
function buildParameterMapObject(name, value) {
return {
map: [
{value: name, type: 'template', key: 'parameter'},
{value: value.toString(), type: 'template', key: 'parameterValue'}
],
type: 'map'
};
}

/**
* Builds the map object for user properties.
* @param {string} name The name of the entity being mapped.
* @param {string} value The value of the entity being mapped.
* @return {!Object} The map object that can be added to a tag or variable.
*/
function buildUserPropertyMapObject (name, value) {
return {
map: [
{value: name, type: 'template', key: 'name'},
{value: value.toString(), type: 'template', key: 'value'}
],
type: 'map'
};
}

/**
Expand Down Expand Up @@ -158,17 +173,17 @@ function writeTagNamesToValidationSheet(tags) {
}

/**
* Writes config tag names to the validation sheet.
* @param {!Array} tags An array of tags in a workspace.
* Writes event setting variable names to the validation sheet.
* @param {!Array} variables An array of variables in a workspace.
*/
function writeConfigTagNamesToValidationSheet(tags) {
const tagNames = tags.reduce((arr, tag) => {
if (tag.type == 'gaawc') {
arr.push([tag.name]);
function writeSettingVariableNamesToValidationSheet(variables) {
const variableNames = variables.reduce((arr, variable) => {
if (variable.type == 'gtes') {
arr.push([`{{${variable.name}}}`]);
}
return arr;
}, []);
writeToSheet(tagNames, 'validation', 'config tag names');
writeToSheet(variableNames, 'validation', 'event setting variable names');
}

/**
Expand Down

0 comments on commit 9a4d06d

Please sign in to comment.