-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.js
74 lines (65 loc) · 2.02 KB
/
template.js
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
const JSON = require('JSON');
const Firestore = require('Firestore');
const getRequestHeader = require('getRequestHeader');
const getAllEventData = require('getAllEventData');
const getTimestampMillis = require('getTimestampMillis');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const getType = require('getType');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
let input = data.addEventData ? getAllEventData() : {};
let options = { merge: !!data.firebaseMerge };
if (data.firebaseProjectIdOverride) options.projectId = data.firebaseProjectId;
if (data.addTimestamp) input[data.timestampFieldName] = getTimestampMillis();
if (data.customDataList) {
data.customDataList.forEach((d) => {
if (data.skipNilValues) {
const dType = getType(d.value);
if (dType === 'undefined' || dType === 'null') return;
}
if (getType(d.name) === 'string' && d.name.indexOf('.') !== -1) {
const nameParts = d.name.split('.');
let obj = input;
for (let i = 0; i < nameParts.length - 1; i++) {
const part = nameParts[i];
if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
}
obj[nameParts[nameParts.length - 1]] = d.value;
} else {
input[d.name] = d.value;
}
});
}
Firestore.write(data.firebasePath, input, options).then((id) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Firestore',
Type: 'Message',
TraceId: traceId,
EventName: 'Write',
DocumentId: id,
DocumentInput: input,
})
);
}
data.gtmOnSuccess();
}, data.gtmOnFailure);
function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}