forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagManager.gs
160 lines (155 loc) · 6.17 KB
/
tagManager.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
/**
* Copyright 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.
*/
// [START apps_script_tag_manager_create_version]
/**
* Creates a container version for a particular account with the input accountPath.
* @param {string} accountPath The account path.
* @return {string} The tag manager container version.
*/
function createContainerVersion(accountPath) {
var date = new Date();
// Creates a container in the account, using the current timestamp to make
// sure the container is unique.
var container = TagManager.Accounts.Containers.create(
{
'name': 'appscript tagmanager container ' + date.getTime(),
'usageContext': ['WEB']
},
accountPath);
var containerPath = container.path;
// Creates a workspace in the container to track entity changes.
var workspace = TagManager.Accounts.Containers.Workspaces.create(
{'name': 'appscript workspace', 'description': 'appscript workspace'},
containerPath);
var workspacePath = workspace.path;
// Creates a random value variable.
var variable = TagManager.Accounts.Containers.Workspaces.Variables.create(
{'name': 'apps script variable', 'type': 'r'},
workspacePath);
// Creates a trigger that fires on any page view.
var trigger = TagManager.Accounts.Containers.Workspaces.Triggers.create(
{'name': 'apps script trigger', 'type': 'PAGEVIEW'},
workspacePath);
// Creates a arbitary pixel that fires the tag on all page views.
var tag = TagManager.Accounts.Containers.Workspaces.Tags.create(
{
'name': 'apps script tag',
'type': 'img',
'liveOnly': false,
'parameter': [
{'type': 'boolean', 'key': 'useCacheBuster', 'value': 'true'}, {
'type': 'template',
'key': 'cacheBusterQueryParam',
'value': 'gtmcb'
},
{'type': 'template', 'key': 'url', 'value': '//example.com'}
],
'firingTriggerId': [trigger.triggerId]
},
workspacePath);
// Creates a container version with the variabe, trigger, and tag.
var version = TagManager.Accounts.Containers.Workspaces
.create_version(
{'name': 'apps script version'}, workspacePath)
.containerVersion;
Logger.log(version);
return version;
}
// [END apps_script_tag_manager_create_version]
// [START apps_script_tag_manager_publish_version]
/**
* Retrieves the container path from a container version path.
* @param {string} versionPath The version path.
* @return {string} The container path.
*/
function grabContainerPath(versionPath) {
var pathParts = versionPath.split('/');
return pathParts.slice(0, 4).join('/');
}
/**
* Publishes a container version publically to the world and creates a quick
* preview of the current container draft.
* @param {object} version The container version.
*/
function publishVersionAndQuickPreviewDraft(version) {
var containerPath = grabContainerPath(version.path);
// Publish the input container version.
TagManager.Accounts.Containers.Versions.publish(version.path);
var workspace = TagManager.Accounts.Containers.Workspaces.create(
{'name': 'appscript workspace', 'description': 'appscript workspace'},
containerPath);
var workspaceId = workspace.path;
// Quick previews the current container draft.
var quickPreview = TagManager.Accounts.Containers.Workspaces.quick_preview(
workspace.path);
Logger.log(quickPreview);
}
// [END apps_script_tag_manager_publish_version]
// [START apps_script_tag_manager_create_user_environment]
/**
* Retrieves the container path from a container version path.
* @param {string} versionPath The version path.
* @return {string} The container path.
*/
function grabContainerPath(versionPath) {
var pathParts = versionPath.split('/');
return pathParts.slice(0, 4).join('/');
}
/**
* Creates and reauthorizes a user environment in a container that points
* to a container version passed in as an argument.
* @param {object} version The container version object.
*/
function createAndReauthorizeUserEnvironment(version) {
// Creates a container version.
var containerPath = grabContainerPath(version.path);
// Creates a user environment that points to a container version.
var environment = TagManager.Accounts.Containers.Environments.create(
{
'name': 'test_environment',
'type': 'user',
'containerVersionId': version.containerVersionId
},
containerPath);
Logger.log('Original user environment: ' + environment);
// Reauthorizes the user environment that points to a container version.
TagManager.Accounts.Containers.Environments.reauthorize({}, environment.path);
Logger.log('Reauthorized user environment: ' + environment);
}
// [END apps_script_tag_manager_create_user_environment]
// [START apps_script_tag_manager_log]
/**
* Logs all emails and container access permission within an account.
* @param {string} accountPath The account path.
*/
function logAllAccountUserPermissionsWithContainerAccess(accountPath) {
var userPermissions =
TagManager.Accounts.User_permissions.list(accountPath).userPermission;
for (var i = 0; i < userPermissions.length; i++) {
var userPermission = userPermissions[i];
if ('emailAddress' in userPermission) {
var containerAccesses = userPermission.containerAccess;
for (var j = 0; j < containerAccesses.length; j++) {
var containerAccess = containerAccesses[j];
Logger.log(
'emailAddress:' + userPermission.emailAddress + ' containerId:' +
containerAccess.containerId + ' containerAccess:' +
containerAccess.permission);
}
}
}
}
// [END apps_script_tag_manager_log]