This script was made by inspiration from Simo Hava's post on a slack notification system for Google Tag Manager changes Its intended use is as a Google Cloud Function integrated with Slack and Microsoft Teams, but you can run the script anywhere you like.
This guide is also available on Knapstad.dev
This script with this setup will check for changes in your Google Tag Manager container and post the changes to Slack and Microsoft Teams.
Slack:
Teams:
This script requires:
Download config.json, main.py, messages.py and requrements.txt to your project folder.
First you want to set up your Google cloud Project. Go to this link create Google Cloud Project
Give you project an awesome name and click create. Remember to set up a billing account if you don't already have one: Create a billing account
There are a couple of APIs' you need to enable for this project to work. The Google Tag Manager API and the Google Cloud Functions API. The easiest way to enable them is to use the search bar on the top of the page. So, with your project selected, search for "Tag manager api":
Click on Tag manager api and on the new screen click the enable button to enable the API.
Do the same for Cloud functions API.
Now that you have enabled the APIs, you need somewhere to store the data. Go to Google Cloud Storage and click Create Bucket.
Chose a unique name for your bucket, this name must be globally unique.
Next you have to choose a region. You can choose whatever region you want but it will affect your latency, so it's recommended to choose one that's also available for cloud functions. For an updated list of Cloud Functions locations follow this link
Keep the rest of the settings as the default. Storage class should be Standard, Access control should be Fine-grained. And then click Create.
Go to the slack app and in the channel you want to add the messages, click the settings cog, and choose add an app.
In the next dialog search for webhook and click install on Incoming Webhook.
Or you can go directly to Slack api page.
On the page click the big add to slack button. Choose witch channel the webhook should connect to and click Add incoming Webhooks integration.
Copy the webhook URL to the config file (We'll go over the config file setup later) Finnish the setup, you can change the name and add a description and customize an icon for your slack bot. If you want to customize the messages more, read the documentation linked from that page.
Open the Teams app and click the ellipsis next to the channel you want to add the integration to and click Connectors.
Next to Incoming Webhook click configure
Give it a name and upload a nice icon and click create
Copy the webhook URL and click done.
Go to Google Cloud and find the service accounts Either create a new one or click on the actions of the default one.
Choose json in the next dialog and click create. This will download a file to your machine. Copy this file to the same directory as your other files for this project.
Copy the email from the service account and add this as a user with read access in GTM
Now it's time to add all our parts to the config file.
{
"bucket_name": "bucket_name",
"blob_name": "state.json",
"slack_hook": "slack_hook_url",
"microsoft_hook": "microsoft_teams_hook_url",
"cloud_credetials": "path_to_credentials_file"
}
Type in your detail in the config file (I'm using dummy values)
{
"bucket_name": "gtm-state",
"blob_name": "state.json",
"slack_hook": "https://my.slack.hook.com",
"microsoft_hook": "https://my.microsoft.hook.com",
"cloud_credetials": "mycredentials.json"
}
First create a zip file of the project. Make shure you only include main.py, messages.py, requirements.txt, config.json and your credentials file.
Go to Google Cloud - cloud functions and click create function.
In the create function dialog pick a name you like. I went with GTM-version.
Set the memory allocation to 256 or 128, the latter is cheaper.
The trigger should be set to Pub/Sub.
Add a new topic, I created gtm-version.
Under source code choose zip-file.
Runtime should be Python 3.7.
Add your zip-file and set your bucket as the state bucket.
The function to execute is main.
In the advanced section set the location to the same as you set for your bucket
Click create and your function is done.
This is what it should look like when you have successfully deployed the function
Your function is up, but as for now you need to manually run it. Head over to the Cloud Scheduler. And click create job. Select a location, i selected one close to where my function and bucket lives.
Click next and wait for the process to finish.
In the next dialog set a project unique name and you can set a description if you want.
Frequency is set using using unix-cron format, I'll run my function every 10 minutes, so I'll use */10 * * * *
. If you need or want to read more about the unix-cron format go here
Set your preferred time zone.
Set the target to Pub/Sub.
Set the topic to the topic you created earlier.
The payload is set to true
Now click create
You are done. The function will check for updates every 10 minutes and post to Slack and Teams if there are any changes.