forked from google/analytics-settings-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·257 lines (240 loc) · 8.37 KB
/
deploy.sh
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
#!/bin/bash
###########################################################################
#
# Copyright 2021 Google Inc.
#
# 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.
#
cat <<END
******** Welcome **********
*
* Google Analytics Settings Database Setup
*
***************************
END
read -p "Please enter your Google Cloud Project ID: " project_id
echo "~~~~~~~~ Enabling APIs ~~~~~~~~~~"
gcloud services enable \
cloudbuild.googleapis.com \
cloudfunctions.googleapis.com \
storage-component.googleapis.com \
storage-api.googleapis.com \
bigquery.googleapis.com \
cloudscheduler.googleapis.com \
appengine.googleapis.com \
analytics.googleapis.com \
analyticsadmin.googleapis.com \
--async
exit_setup () {
exit "Exiting Google Analytics Settings Database setup. Setup failed."
}
cloud_bucket_setup () {
read -p "Please enter your Cloud Bucket name: " cloud_bucket_name
echo "~~~~~~~~ Creating Cloud Bucket ~~~~~~~~~~"
if gsutil mb gs://$cloud_bucket_name; then
echo "Bucket created."
else
read -p "Bucket creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
cloud_bucket_setup
fi
fi
}
cloud_bucket_setup
create_service_account () {
gcloud iam service-accounts create $service_account_name \
--display-name=$service_account_name
}
set_service_account_email () {
if [[ service_account_email = "" || $1 < 3 ]]; then
echo "Service account email attempt $1"
sleep 2
retry_attempt=$(( $1 + 1 ))
set_service_account_email "$retry_attempt"
else
echo $service_account_email
fi
}
set_service_account_iam_policy () {
gcloud projects add-iam-policy-binding $project_id \
--member="serviceAccount:$service_account_email" \
--role="roles/editor"
}
service_account_setup () {
read -p "Please enter you desired service account name with no spaces.
This service account will be used by your Cloud Function.
The recommended name is 'ga-database' : " service_account_name
echo "~~~~~~~~ Creating Service Account ~~~~~~~~~~"
if create_service_account; then
service_account_email="$service_account_name@$project_id.iam.gserviceaccount.com"
if set_service_account_iam_policy; then
echo "Service account created."
else
read -p "Service account creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
service_account_setup
fi
fi
else
read -p "Service account creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
create_service_account
fi
fi
}
service_account_setup
create_cloud_function () {
gcloud functions deploy $function_name \
--project=$project_id \
--runtime=python39 \
--service-account=$service_account_email \
--memory=1GB \
--timeout=540s \
--trigger-http \
--entry-point=ga_settings_download \
--set-env-vars=BUCKET_NAME=$cloud_bucket_name
}
cloud_function_setup () {
read -p "Please enter your desired Function name. The recommended
function name is 'analytics_settings_downloader': " function_name
cd settings_downloader_function
echo "~~~~~~~~ Creating Function ~~~~~~~~~~"
if create_cloud_function; then
cd ..
echo "Function created."
else
cd ..
read -p "Function creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
cloud_function_setup
fi
fi
}
cloud_function_setup
echo "~~~~~~~~ Creating BigQuery Dataset ~~~~~~~~~~"
bq mk -d $project_id:analytics_settings_database
echo "~~~~~~~~ Creating BigQuery Tables ~~~~~~~~~~"
cd schemas
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_account_summaries_schema.json \
$project_id:analytics_settings_database.ua_account_summaries
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_goals_schema.json \
$project_id:analytics_settings_database.ua_goals
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_views_schema.json \
$project_id:analytics_settings_database.ua_views
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_filters_schema.json \
$project_id:analytics_settings_database.ua_filters
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_filter_links_schema.json \
$project_id:analytics_settings_database.ua_filter_links
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_segments_schema.json \
$project_id:analytics_settings_database.ua_segments
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_custom_dimensions_schema.json \
$project_id:analytics_settings_database.ua_custom_dimensions
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_custom_metrics_schema.json \
$project_id:analytics_settings_database.ua_custom_metrics
bq mk -t --time_partitioning_type=DAY \
--schema=./ua_audiences_schema.json \
$project_id:analytics_settings_database.ua_audiences
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_account_summaries_schema.json \
$project_id:analytics_settings_database.ga4_account_summaries
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_accounts_schema.json \
$project_id:analytics_settings_database.ga4_accounts
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_properties_schema.json \
$project_id:analytics_settings_database.ga4_properties
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_data_streams_schema.json \
$project_id:analytics_settings_database.ga4_data_streams
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_measurement_protocol_secrets_schema.json \
$project_id:analytics_settings_database.ga4_measurement_protocol_secrets
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_conversion_events_schema.json \
$project_id:analytics_settings_database.ga4_conversion_events
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_custom_dimensions_schema.json \
$project_id:analytics_settings_database.ga4_custom_dimensions
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_custom_metrics_schema.json \
$project_id:analytics_settings_database.ga4_custom_metrics
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_firebase_links_schema.json \
$project_id:analytics_settings_database.ga4_firebase_links
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_google_ads_links_schema.json \
$project_id:analytics_settings_database.ga4_google_ads_links
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_dv360_link_proposals_schema.json \
$project_id:analytics_settings_database.ga4_dv360_link_proposals
bq mk -t --time_partitioning_type=DAY \
--schema=./ga4_dv360_links_schema.json \
$project_id:analytics_settings_database.ga4_dv360_links
cd ..
echo "BigQuery tables created."
create_cloud_scheduler () {
gcloud scheduler jobs create http $scheduler_name \
--schedule "0 23 * * *" \
--uri="$function_uri" \
--http-method=GET \
--oidc-service-account-email=$service_account_email \
--oidc-token-audience=$function_uri \
--project=$project_id
}
cloud_scheduler_setup () {
read -p "Please enter your desired Cloud Scheduler name.
The recommended scheduler name is 'analytics_settings_downloader': " scheduler_name
echo "A cloud scheduler will now be created that runs daily at 11 PM."
echo "~~~~~~~~ Creating Cloud Scheduler ~~~~~~~~~~"
function_uri=$(gcloud functions describe $function_name --format="value(httpsTrigger.url)")
echo $function_uri
if gcloud app browse; then
gcloud app create
fi
if create_cloud_scheduler; then
echo "Cloud scheduler created."
else
cd ..
read -p "Schedule job creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
cloud_scheduler_setup
fi
fi
}
cloud_scheduler_setup
echo "***************************
*
* Google Analytics Settings Database Setup Complete!
*
* You must now grant $service_account_email access to your Google Analytics
* Accounts. This will be the email Google Cloud uses to access your Google
* Analytics settings.
***************************"