Learn business growth with Google Analytics 4 Forums Google Analytics 4 Utilizing Google Tag Manager's REST APIs to Generate GA4 Tags Reply To: Utilizing Google Tag Manager's REST APIs to Generate GA4 Tags

  • Emma

    Member
    27 June 2023 at 7:24 am

    Sure, I totally feel where you’re coming from and I think I have an simple approach for you.

    Essentially, although the GTM API tutorial does a great job of showing how to create a basic ‘Tag’, it doesn’t quite cover everything, especially for more nuanced tags like GA4 Event Tag. But don’t worry, we just need to dive a bit deeper.

    Here’s my preferred method: I’d suggest creating a tag within the UI first. Then, export it and have a look at the JSON structure. This’ll serve as your model when assembling your own. Make sense?

    Check out this example of the GA4 Event Tag JSON from an export:
    `json
    {
    “accountId”: “accountId”,
    “containerId”: “containerId”,
    “tagId”: “6”,
    “name”: “tagName”,
    “type”: “gaawe”,
    “parameter”: [
    {
    “type”: “TEMPLATE”,
    “key”: “eventName”,
    “value”: “stay_time”
    },
    {
    “type”: “LIST”,
    “key”: “eventParameters”,
    “list”: [
    {
    “type”: “MAP”,
    “map”: [
    {
    “type”: “TEMPLATE”,
    “key”: “name”,
    “value”: “abc”
    },
    {
    “type”: “TEMPLATE”,
    “key”: “value”,
    “value”: “100”
    }
    ]
    }
    ]
    },
    {
    “type”: “TAG_REFERENCE”,
    “key”: “measurementId”,
    “value”: “GA4_G-000000000”
    }
    ],
    “fingerprint”: “1657085557584”,
    “firingTriggerId”: [
    “0”
    ],
    “tagFiringOption”: “ONCE_PER_LOAD”,
    “monitoringMetadata”: {
    “type”: “MAP”
    },
    “consentSettings”: {
    “consentStatus”: “NOT_SET”
    }
    }
    `
    Based on what you’re trying to do, you may be missing this bit:
    `json
    {
    “type”: “TAG_REFERENCE”,
    “key”: “measurementId”,
    “value”: “GA4_G-000000000”
    }
    `
    This value refers to the name of the GA4 config tag. If you don’t have that, you’d want to set up the GA4 Config tag beforehand, you can do that through the UI or API.

    Here’s an example of what the GA4 config tag setup could look like:
    `json
    {
    “accountId”: “{{accountId}}”,
    “containerId”: “{{containerId}}”,
    “tagId”: “4”,
    “name”: “{{tag_name}}”,
    “type”: “gaawc”,
    “priority”: {
    “type”: “INTEGER”,
    “value”: “1000”
    },
    “parameter”: [
    {
    “type”: “BOOLEAN”,
    “key”: “sendPageView”,
    “value”: “true”
    },
    {
    “type”: “BOOLEAN”,
    “key”: “enableSendToServerContainer”,
    “value”: “false”
    },
    {
    “type”: “TEMPLATE”,
    “key”: “measurementId”,
    “value”: “{{measurementId}}”
    }
    ],
    “fingerprint”: “1663432050222”,
    “firingTriggerId”: [
    “2147479553”
    ],
    “tagFiringOption”: “ONCE_PER_LOAD”,
    “monitoringMetadata”: {
    “type”: “MAP”
    },
    “consentSettings”: {
    “consentStatus”: “NOT_SET”
    }
    }
    `
    To get this up and running, go through and replace the parts labeled {{variable}} with your own values. And with that, you should be good to go!

    Hopefully, this clarifies things a bit and makes your transition over to GA4 a little easier. You’re nailing it!