-
Notifications
You must be signed in to change notification settings - Fork 2
/
11_Event-Parameter-Join-Google-Sheet.gs
358 lines (307 loc) · 15.8 KB
/
11_Event-Parameter-Join-Google-Sheet.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* Copyright 2024 Knowit Experience Oslo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let manualSheetTrigger = false;
function writeEventsToSheetManually() {
try{
manualSheetTrigger = true;
eventDocumentationToSheet(manualSheetTrigger);
}catch(err){
Logger.log('writeEventsToSheetManually: '+err.stack);
if(manualSheetTrigger) {
SpreadsheetApp.getUi().alert('Error occoured in "writeEventsToSheetManually" function: \n'+err);
}
}
}
function eventDocumentationToSheet() {
try{
const ss = SpreadsheetApp.getActiveSpreadsheet();
const eventDocumentationSheet = ss.getSheetByName('EventDataSource');
if (!eventDocumentationSheet) {
// If the sheet doesn't exist, create it
ss.insertSheet('EventDataSource');
}
// Clear existing content and set headers
eventDocumentationSheet.clear();
eventDocumentationSheet.getRange('A1:M1').setValues([['Event Group', 'Event Name', 'Event Method', 'Event Type', 'Event Conversion', 'Event Conversion Counting', 'Event Description', 'Event Comment', 'Event Time Edited', 'Event Website', 'Event iOS App', 'Event Android App', 'Event GTM Comment']]);
const eventDocumentation = [];
eventRows.forEach((row) => {
if (row[eventNameColumn - 1] !== 'ga4_config') {
const event_group = row[eventGroupColumn - 1].trim();
const event_name = row[eventNameColumn - 1].trim();
const event_method = row[eventMethodColumn - 1].trim();
const event_type = row[eventTypeColumn - 1].trim();
const event_conversion = row[eventConversionColumn - 1];
const event_conversion_counting = row[eventConversionCountingColumn - 1].trim();
const event_description = row[eventDescriptionColumn - 1].trim();
const event_comment = row[eventCommentColumn - 1].trim();
const event_time_edited = row[eventEditedTimeColumn- 1] ? Utilities.formatDate(row[eventEditedTimeColumn- 1], timezone, dateFormat+"' 'HH:mm:ss") : '';
const event_website = row[eventPlatformWebsiteColumn - 1];
const event_ios_app = row[eventPlatformIosColumn - 1];
const event_android_app = row[eventPlatformAndroidColumn - 1];
const event_gtm_comment = row[eventGTMCommentColumn - 1].trim();
if((event_group && event_name && event_description) && (event_website || event_ios_app || event_android_app)) {
eventDocumentation.push([
event_group,event_name,event_method,event_type,event_conversion,event_conversion_counting,event_description,event_comment,event_time_edited,event_website,event_ios_app,event_android_app,event_gtm_comment
]);
}
}
});
// Write data to the sheet
if (eventDocumentation.length > 0) {
const range = eventDocumentationSheet.getRange(2, 1, eventDocumentation.length, 13);
range.setValues(eventDocumentation);
}
// Create Image Documentation for Events
eventImageDocumentationToSheet();
}catch(err){
Logger.log('eventDocumentationToSheet: '+err.stack);
if(manualSheetTrigger) {
SpreadsheetApp.getUi().alert('Error occoured in "eventDocumentationToSheet" function: \n'+err);
}
}
}
function eventImageDocumentationToSheet() {
try{
function fixDriveImageURL(event_image) {
if (event_image.startsWith('https://drive.google.com/file/d/')) {
const fileIdStartIndex = event_image.indexOf('/d/') + 3;
const fileIdEndIndex = event_image.indexOf('/view');
const fileId = event_image.substring(fileIdStartIndex, fileIdEndIndex);
return 'https://drive.google.com/uc?id=' + fileId;
} else {
return event_image;
}
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
const eventImageDocumentationSheet = ss.getSheetByName('EventImagesDataSource');
if (!eventImageDocumentationSheet) {
// If the sheet doesn't exist, create it
ss.insertSheet('EventImagesDataSource');
}
// Clear existing content and set headers
eventImageDocumentationSheet.clear();
eventImageDocumentationSheet.getRange('A1:B1').setValues([['Event Name', 'Event Image Documentation URL']]);
const eventImageDocumentation = [];
eventRows.forEach((row) => {
if (row[eventImageDocumentationColumn - 1] && row[eventImageDocumentationColumn - 1].length) {
const event_name = row[eventNameColumn - 1].trim();
const event_images = row[eventImageDocumentationColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",");
const fixed_event_images = event_images.map(event_image => fixDriveImageURL(event_image));
// Push each event name and fixed event image URL pair as a separate row
fixed_event_images.forEach(image => {
eventImageDocumentation.push([
event_name,
image
]);
});
}
});
// Write data to the sheet
if (eventImageDocumentation.length > 0) {
const range = eventImageDocumentationSheet.getRange(2, 1, eventImageDocumentation.length, 2);
range.setValues(eventImageDocumentation);
}
// Create Parameter Documentation
parameterDocumentationToSheet();
}catch(err){
Logger.log('eventImageDocumentationToSheet: '+err.stack);
if(manualSheetTrigger) {
SpreadsheetApp.getUi().alert('Error occoured in "eventImageDocumentationToSheet" function: \n'+err);
}
}
}
function parameterDocumentationToSheet() {
try{
const parameterSheetData = [];
parameterRows.forEach((row) => {
const parameter = {
parameterGroup: row[parameterGroupColumn-1].trim(),
parameterDisplayName: row[parameterDisplayNameColumn-1].trim(),
parameterName: row[parameterNameColumn-1].trim(),
parameterScope: row[parameterScopeColumn-1].trim(),
parameterType: row[parameterTypeColumn-1].trim(),
parameterFormat: row[parameterFormatColumn-1].trim(),
parameterDisallowAdsPersonalization: row[parameterDisallowAdsPersonalizationColumn-1] ? true : false,
parameterExampleValue: row[parameterExampleValueColumn-1].trim(),
parameterDescription: row[parameterDescriptionColumn-1].trim(),
parameterGTMComment: row[parameterGTMCommentColumn-1].trim()
}
parameterSheetData.push(parameter);
});
const eventParameterObjects = []; // This will hold your resulting objects
let ga4EventConfigParameters = []; // This will store parameters of 'ga4_config' with an additional marker
// Isolate 'ga4_config' Event Scoped Parameters
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName === 'ga4_config') {
ga4EventConfigParameters = row[eventParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",").map(parameterName => ({
parameterName: parameterName,
isGa4Config: true // Mark as originating from 'ga4_config'
}));
}
});
// Then, process other events and add 'ga4_config' parameters to them
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName !== 'ga4_config') {
let parameters = row[eventParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",");
// Convert current event parameters to objects and mark them
const currentEventParameters = parameters.map(parameterName => ({
parameterName: parameterName,
isGa4Config: false // These do not originate from 'ga4_config'
}));
// Combine 'ga4_config' parameters with the current event's parameters
const combinedParameters = currentEventParameters.concat(ga4EventConfigParameters);
// For each combined parameter, create a new object with eventName, that parameter, and the isGa4Config flag
combinedParameters.forEach(({ parameterName, isGa4Config }) => {
const eventParameterObject = {
eventName: eventName,
parameterName: parameterName,
parameterScope: 'EVENT',
isGa4Config: isGa4Config // Indicates if this parameter was originally a 'ga4_config' parameter
};
eventParameterObjects.push(eventParameterObject);
});
}
});
const userParameterObjects = []; // This will hold your resulting objects
let ga4UserConfigParameters = []; // This will store parameters of 'ga4_config' with an additional marker
// Isolate 'ga4_config' User Scoped Parameters
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName === 'ga4_config') {
ga4UserConfigParameters = row[eventUserParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",").map(parameterName => ({
parameterName: parameterName,
isGa4Config: true // Mark as originating from 'ga4_config'
}));
}
});
// Then, process other events and add 'ga4_config' parameters to them
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName !== 'ga4_config') {
let parameters = row[eventUserParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",");
// Convert current event parameters to objects and mark them
const currentUserParameters = parameters.map(parameterName => ({
parameterName: parameterName,
isGa4Config: false // These do not originate from 'ga4_config'
}));
// Combine 'ga4_config' parameters with the current event's parameters
const combinedParameters = currentUserParameters.concat(ga4UserConfigParameters);
// For each combined parameter, create a new object with eventName, that parameter, and the isGa4Config flag
combinedParameters.forEach(({ parameterName, isGa4Config }) => {
const userParameterObject = {
eventName: eventName,
parameterName: parameterName,
parameterScope: 'USER',
isGa4Config: isGa4Config // Indicates if this parameter was originally a 'ga4_config' parameter
};
userParameterObjects.push(userParameterObject);
});
}
});
const itemParameterObjects = []; // This will hold your resulting objects
let ga4ItemConfigParameters = []; // This will store parameters of 'ga4_config' with an additional marker (those shouldn't exist, but the code supports it)
// Isolate 'ga4_config' Item Scoped Parameters
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName === 'ga4_config') {
ga4ItemConfigParameters = row[eventItemParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",").map(parameterName => ({
parameterName: parameterName,
isGa4Config: true // Mark as originating from 'ga4_config'
}));
}
});
// Then, process other events and add 'ga4_config' parameters to them
eventRows.forEach((row) => {
const eventName = row[eventNameColumn - 1].trim();
if (eventName !== 'ga4_config') {
let parameters = row[eventItemParametersColumn - 1].trim().replace(/\r\n|\n|' '/g, ',').split(",");
// Convert current event parameters to objects and mark them
const currentItemParameters = parameters.map(parameterName => ({
parameterName: parameterName,
isGa4Config: false // These do not originate from 'ga4_config'
}));
// Combine 'ga4_config' parameters with the current event's parameters
const combinedParameters = currentItemParameters.concat(ga4ItemConfigParameters);
// For each combined parameter, create a new object with eventName, that parameter, and the isGa4Config flag
combinedParameters.forEach(({ parameterName, isGa4Config }) => {
const itemParameterObject = {
eventName: eventName,
parameterName: parameterName,
parameterScope: 'ITEM',
isGa4Config: isGa4Config // Indicates if this parameter was originally a 'ga4_config' parameter
};
itemParameterObjects.push(itemParameterObject);
});
}
});
const parameterConcat = eventParameterObjects.concat(userParameterObjects,itemParameterObjects);
let uniqueParameters = new Set();
const parameterDocumentation = [];
parameterSheetData.forEach((sheetData) => {
parameterConcat.forEach((parameterConcat) => {
if(sheetData.parameterName === parameterConcat.parameterName && sheetData.parameterScope === parameterConcat.parameterScope) {
// Create a unique key for each combination of parameterName and parameterScope
const uniqueKey = sheetData.parameterName + '_' + sheetData.parameterScope + '_' + parameterConcat.eventName;
// Check if this combination has already been added
if (!uniqueParameters.has(uniqueKey)) {
uniqueParameters.add(uniqueKey); // Add the unique key to the set
const parameter_group = sheetData.parameterGroup;
const parameter_display_name = sheetData.parameterDisplayName;
const parameter_name = sheetData.parameterName;
const parameter_scope = sheetData.parameterScope;
const parameter_type = sheetData.parameterType;
const parameter_format = sheetData.parameterFormat;
const parameter_disallow_ads_personalization = sheetData.parameterDisallowAdsPersonalization;
const parameter_example_value = sheetData.parameterExampleValue;
const parameter_description = sheetData.parameterDescription;
const parameter_gtm_comment = sheetData.parameterGTMComment;
const event_name = parameterConcat.eventName;
const ga4_config_parameter = parameterConcat.isGa4Config;
if(parameter_group && parameter_display_name && parameter_name && parameter_description) {
parameterDocumentation.push([
parameter_group,parameter_display_name,parameter_name,parameter_scope,parameter_type,parameter_format,parameter_disallow_ads_personalization,parameter_example_value,parameter_description,parameter_gtm_comment,event_name,ga4_config_parameter
]);
}
}
}
});
});
const ss = SpreadsheetApp.getActiveSpreadsheet();
const parameterDocumentationSheet = ss.getSheetByName('ParameterDataSource');
if (!parameterDocumentationSheet) {
// If the sheet doesn't exist, create it
ss.insertSheet('ParameterDataSource');
}
// Clear existing content and set headers
parameterDocumentationSheet.clear();
parameterDocumentationSheet.getRange('A1:L1').setValues([['Parameter Group', 'Parameter Display Name', 'Parameter Name', 'Parameter Scope', 'Parameter Type', 'Parameter Format', 'Parameter NPA', 'Parameter Example Value', 'Parameter Description', 'Parameter GTM Comment', 'Event Name', 'GA4 Config Parameter']]);
// Write data to the sheet
if (parameterDocumentation.length > 0) {
const range = parameterDocumentationSheet.getRange(2, 1, parameterDocumentation.length, 12);
range.setValues(parameterDocumentation);
}
if(manualSheetTrigger) {
SpreadsheetApp.getUi().alert('Event & Parameter Documentation has been added to Data Source Sheets.');
}
}catch(err){
Logger.log('parameterDocumentationToSheet: '+err.stack);
if(manualSheetTrigger) {
SpreadsheetApp.getUi().alert('Error occoured in "parameterDocumentationToSheet" function: \n'+err);
}
}
}