To generate a GA4 tag using the Google Tag Manager REST APIs, follow these steps:
Here's some sample code to create a new GA4 tag using the GTM API:
import google.auth
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# Set up API credentials
creds = Credentials.from_authorized_user_info(info={...})
service = build('tagmanager', 'v2', credentials=creds)
# Specify tag settings
tag = {
'name': 'MyGA4Tag',
'type': 'GA4_GLOBAL_TAG',
'ga4_property_id': 'G-XXXXXXXXXX',
'parameter': [{
'type': 'BOOLEAN',
'key': 'enabled',
'value': True
}]
}
# Create new tag in GTM container
try:
response = service.accounts().containers().tags().create(
parent='accounts/123456/containers/654321',
body=tag
).execute()
print(response)
except HttpError as error:
print('An error occurred: %s' % error)
In this example, the creds
variable is the authenticated credentials object obtained from the Google Cloud Console. The tag
variable contains the settings for the new GA4 tag. The parent
parameter specifies the GTM container in which to create the tag. Finally, the execute()
method sends the request to the API to create the tag, and the response is printed to the console.
For more information on using the GTM API to create tags, see the official documentation: https://developers.google.com/tag-manag….
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-01 11:00:00 +0000
Seen: 15 times
Last updated: Nov 21 '22
How to perform a historical backfill from GA4 into BigQuery?
Can you restate the GA4 Enhanced Ecommerce concepts of "view_item_list" and "add_to_cart"?
What is the distinct identifier present in BigQuery GA4 events data?
What is the difference between using a key in an imported JSX tag and writing out the full code?
What is the method for locating the <li class> tag with Selenium?
What is the time stamp for when the tag value transitions between 1 and 2?
What is the process of utilizing objectFit in Next.js 13 with the <Image> tag?
What is the process for pushing a tag to a remote Git repository?
What causes the <img> tag to only display the image shortcut instead of the actual image?