In this guide, we will walk you through how to access your Google Tag Manager accounts and container and send data to google sheets. This article build upon our initial guide on how to access GTM using the API provided by Google.
Google Tag Manager To Google Sheets: Overview
The goal of this project is to create a Python application that authenticates with the Google Tag Manager API, retrieves account details, formats the data, and writes it to a Google Sheet. We will stick with super simple examples in this guide to guide slowly into the weeds of working with the API.
Perquisites before getting started
First, please make sure to have a Google cloud project with a service account email added to your GTM accounts. You can add browse to Google cloud by following this link.
Also, You need to download the secret key after setting up a service account. The file contains the Google service account credentials, which are required for authenticating with both the GTM and Google Sheets APIs.
Let’s break down the key components of the project, including authentication, data retrieval, and data writing.
1. Google Tag Manager Authentication
First, we need to authenticate with the GTM API using a service account. This is a special type of Google account designed for backend scripts (like ours). When you create this service account in your Google Cloud project, you also download a .json
file — this file contains the secret keys your code needs to log in.
import os from google.oauth2 import service_account from googleapiclient.discovery import build def gtm_auth(): SCOPES = ["https://www.googleapis.com/auth/tagmanager.readonly"] #define read permission as scope for GTM access credentials = os.getenv('CREDENTIALS') #this is the path to secret key downloaded from GCP credentials = service_account.Credentials.from_service_account_file( filename=credentials, scopes=SCOPES ) service = build('tagmanager', 'v2', credentials=credentials) return service
This function loads the credentials, authenticates with the GTM API, and returns a service object for making API calls.
2. Fetching Google Tag Manager Accounts
Next, we can retrieve GTM accounts and formats them for writing to Google Sheets. Here is an example of how to get accounts information, for example:
def get_accounts(service): accounts = service.accounts().list().execute() return accounts def get_account_by_id(service, accounts): accounts_list = [["Account Name", "Account ID"]] # Header row account_list = accounts.get('account', []) for account in account_list: account_id = account.get("accountId", "") account_name = account.get("name", "") accounts_list.append([account_name, account_id]) return accounts_list
This code fetches all GTM accounts and creates a list of lists containing account names and IDs, ready for Google Sheets.
3. Google Sheets Authentication
After getting GTM data, we need to also authenticates with the Google Sheets API. The process is quite similar to GTM authentication :
import os from google.oauth2 import service_account from googleapiclient.discovery import build def sheets_auth(): SCOPES = ["https://www.googleapis.com/auth/spreadsheets"] credentials = "/path/key.json" sheets_credentials = service_account.Credentials.from_service_account_file( filename=credentials, scopes=SCOPES ) sheets_service = build('sheets', 'v4', credentials=sheets_credentials) return sheets_service
This function authenticates with the Google Sheets API using the key we downloaded earlier. It loads credentials from a .json key file, builds the Sheets service with the required scope, and returns an authenticated service object that allows reading and writing data to Google Sheets.
4. Writing to Google Sheets
Finally, we are ready to start sending our data to our sheet. The code below writes the formatted GTM account data to a specified Google Sheet:
def write_to_sheet(service, spreadsheet_id, range_name, values): body = {'values': values} result = service.spreadsheets().values().update( spreadsheetId=spreadsheet_id, range_name='Sheet1!A1', valueInputOption="RAW", body=body ).execute() return result
In order for this to work, you need to have a valid Google sheets ID. It’s the random characters after the /d/
parameter in the sheet’s URL.
The write_to_sheet function sends data to a specified Google Sheet range using the Sheets API. It takes the sheet service, spreadsheet ID, target range, and data values, then formats the data into a request body. Using the update method with RAW input mode, it writes the values directly to the sheet starting from cell A1.
Conclusion
This guide provided a good base to get you started with getting your data out of Google sheets. Of course, there is really nothing you can’t do with the available data from GTM. You can control the output anyway you want before having your data in google sheets.
Of course, this is only the beginning, in later posts, we will show you more advanced tips on how to use the GTM API.