-
Notifications
You must be signed in to change notification settings - Fork 2
/
callGoogleAPIs.gs
204 lines (154 loc) · 5.78 KB
/
callGoogleAPIs.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
function callGoogleAPIs(config) {
var config = config;
var folders, triggers = {}, tags, tagFiringTriggerReference = {}, tagBlockingTriggerReference = {},
data = {
confluenceAPIToken: config.confluenceAPIToken,
confluencePageId: config.confluencePageId,
confluencePageName: config.confluencePageName,
confluenceDomain: config.confluenceDomain,
container: '',
version: '',
tags: [],
variables: [],
triggers: []
};
// get the google analytics custom dimensions and re-map
var cds = Analytics.Management.CustomDimensions.list(`${config.gaAccountId}`, `${config.gaMeasurementId}`)
.items
.map(function (view) {
return {
index: view.index,
name: view.name
}
});
var metrics = Analytics.Management.CustomMetrics.list(`${config.gaAccountId}`, `${config.gaMeasurementId}`)
.items
.map(function (view) {
return {
index: view.index,
name: view.name
}
});
// get live gtm container
var latestVersion = TagManager.Accounts.Containers.Versions.live(`accounts/${config.gtmAccountId}/containers/${config.gtmContainerId}`);
folders = latestVersion.folder;
tags = latestVersion.tag;
data.container = latestVersion.container.publicId;
data.version = latestVersion.containerVersionId;
// build a simple triggers lookup
for (const { triggerId, name, type } of latestVersion.trigger) {
triggers[triggerId] = { name: name, type: type }
}
// add each tag to the data object
tags
.forEach(tag => {
// trigger logic to return trigger names rather than ids
// loop through trigger Ids - check if id exists in lookup object - return the trigger name from lookup object
let firing = [];
try {
for (let id of tag.firingTriggerId) {
if (triggers[id]) {
firing.push(triggers[id])
if (!tagFiringTriggerReference[id]) {
tagFiringTriggerReference[id] = [tag.name];
} else {
tagFiringTriggerReference[id].push(tag.name);
}
}
}
} catch (e) {
}
// blocking trigger logic to return blocking trigger names rather than ids
// loop through blocking trigger Ids - check if id exists in lookup object - return the trigger name from lookup object
let blocking = [];
try {
for (let id of tag.blockingTriggerId) {
if (triggers[id]) {
blocking.push(triggers[id])
if (!tagBlockingTriggerReference[id]) {
tagBlockingTriggerReference[id] = [tag.name];
} else {
tagBlockingTriggerReference[id].push(tag.name);
}
}
}
} catch (e) {
}
// logic to get dimension names from GA API
// loop through tag.parameter array filtered to dimension mapping - loop through tag.parameter[i].list - loop through tag.parameter[i].list[i].map
// get the dimension index
// filter the custom dimensions object by the dimension index and push to gaDimensions array
let gaDimensions = []
for (const { list = [] } of tag.parameter.filter(param => param.key == 'dimension')) {
for (const { map } of list) {
try {
let localMapping = map;
let index = localMapping.filter(lm => lm.key == 'index')[0].value;
gaDimensions.push(cds.filter(dimension => dimension.index == index)[0])
} catch (e) {
}
}
}
let gaMetrics = []
for (const { list = [] } of tag.parameter.filter(param => param.key == 'metric')) {
for (const { map } of list) {
try {
let localMapping = map;
let index = localMapping.filter(lm => lm.key == 'index')[0].value;
gaMetrics.push(metrics.filter(met => met.index == index)[0])
} catch (e) {
}
}
}
// push folder name, tag type, tag name, firing & blocking triggers and parameter mapping
data.tags.push([
((folders.filter(f => {
return f.folderId == tag.parentFolderId;
})[0] || {}).name || 'Unfiled Item'),
tag.type,
tag.name,
JSON.stringify({ firing: firing, except: blocking }, null, 2),
JSON.stringify(tag.parameter, null, 2),
JSON.stringify(gaDimensions, null, 2),
JSON.stringify(gaMetrics, null, 2),
])
})
// add triggers to data object
latestVersion.trigger
.forEach(trigger => {
// get tags which reference this trigger
let firing = tagFiringTriggerReference[trigger.triggerId] || [];
// get tags which reference this trigger for blocking
let blocking = tagBlockingTriggerReference[trigger.triggerId] || [];
data.triggers.push([
((folders.filter(f => {
return f.folderId == trigger.parentFolderId;
})[0] || {}).name || ''),
trigger.type,
trigger.name,
JSON.stringify({ firing: firing, except: blocking }, null, 2),
JSON.stringify(trigger.customEventFilter, null, 2)
])
})
// add variables to data object
latestVersion.variable
.forEach(macro => {
var references = tags
.filter(tag => {
return JSON.stringify(tag).includes(macro.name)
}).map(t => {
return { name: t.name, type: t.type };
})
data.variables.push([
((folders.filter(f => {
return f.folderId == macro.parentFolderId;
})[0] || {}).name || ''),
macro.type,
macro.name,
JSON.stringify(macro.parameter, null, 2),
JSON.stringify(references, null, 2)
])
})
console.log('documented: ', { data: { tags: data.tags.length, triggers: data.triggers.length, variables: data.variables.length } });
uploadToConfluenceSDR(data);
}