-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_document_from_firestore.tpl
226 lines (197 loc) · 5.37 KB
/
get_document_from_firestore.tpl
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
___INFO___
{
"type": "MACRO",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Artemis - Retrieve Firestore Document",
"description": "Returns document from Firebase",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "gcpProjectId",
"displayName": "GCP Project ID (Where Firestore is located)",
"simpleValueType": true,
"notSetText": "projectId is retrieved from the environment variable GOOGLE_CLOUD_PROJECT",
"help": "Google Cloud Project ID where the Firestore database with margin data is located, leave empty to read from GOOGLE_CLOUD_PROJECT environment variable",
"canBeEmptyString": true
},
{
"type": "TEXT",
"name": "collectionId",
"displayName": "Firestore Collection ID",
"simpleValueType": true,
"defaultValue": "products",
"help": "The collection in Firestore that contains the products"
},
{
"type": "TEXT",
"name": "documentID",
"displayName": "Firestore Document ID",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "zeroIfNotFound",
"checkboxText": "Zero if not found",
"simpleValueType": true,
"defaultValue": true,
"help": "If true items that cannot be found in Firestore will be 0. If false items that cannot be found in Firestore will have their original value from the event data.",
"alwaysInSummary": true
}
]
___SANDBOXED_JS_FOR_SERVER___
// Copyright 2024 Google LLC
//
// 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.
//
/**
* @fileoverview sGTM variable tag that retrieves a document from Firestore
* @version 1.0.0
*/
const Firestore = require('Firestore');
const Promise = require('Promise');
const logToConsole = require('logToConsole');
const makeString = require('makeString');
const getType = require('getType');
const JSON = require('JSON');
/**
* Use Firestore to determine what the new value should be for this item.
* @param {String} item - documentID to retrieve from Firestore
*/
function getFirestoreDocument(documentId) {
let document;
if (!documentId) {
logToConsole("No document Id");
return document;
}
const path = data.collectionId + "/" + documentId;
// Mock API returns a function, whereas usual import is object.
// This logic enables support for tests within the function. See
// https://developers.google.com/tag-platform/tag-manager/server-side/api#mock
let firestore = Firestore;
if (getType(Firestore) === "function"){
firestore = Firestore();
}
return Promise.create((resolve) => {
return firestore.read(path, { projectId: data.gcpProjectId })
.then((result) => {
if (result.data != null) {
document = result.data;
} else {
logToConsole("Firestore document " + data.documentId + " does not exist");
document = "";
}
})
.catch((error) => {
logToConsole("Error retrieving Firestore document `" + path + "`", error);
})
.finally(() => {
resolve(document);
});
});
}
// Entry point
const documentId = data.documentID;
let document = getFirestoreDocument(documentId).then(document => {
return JSON.stringify(document);
}).catch((error) => {
logToConsole("Error", error);
});
return document;
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "all"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "access_firestore",
"versionId": "1"
},
"param": [
{
"key": "allowedOptions",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "projectId"
},
{
"type": 1,
"string": "path"
},
{
"type": 1,
"string": "operation"
}
],
"mapValue": [
{
"type": 1,
"string": "GOOGLE_CLOUD_PROJECT"
},
{
"type": 1,
"string": "*"
},
{
"type": 1,
"string": "read"
}
]
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios: []
___NOTES___
Created on 8/3/2022, 3:02:04 PM