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

  • Utilizing Google Tag Manager's REST APIs to Generate GA4 Tags

    Posted by Finley on 8 October 2022 at 3:50 am

    Hey, we’ve got a bit of a situation. We love our website and now we’re looking to get our Universal Analytics tags switched over to GA4 tags, but we’re all about that work smarter not harder lifestyle – no manual migrating for us! We stumbled upon these super nifty Google Tag Manager (GTM) REST APIs for creating tags, triggers, and so on, and it’s been pretty great so far. In fact, I got really stoked because I was able to whip up a Universal Tag using the REST API, you can check out how I did it here.

    But I’ve hit a bit of a roadblock, I can’t seem to create GA4 tags from the GTM REST APIs. Here’s what I tried:

    {
      "name": "GA4 Tag",
      "type": "gaawe",
      "parameter": [
        {
          "type": "template",
          "key": "measurement_id",
          "value": "G-1234567890"
        },
        {
          "type": "template",
          "key": "event_name",
          "value": "page_view"
        },
        {
          "type": "list",
          "key": "custom_dimensions"
        }
      ]
    }
    

    And BAM! A 400 Bad request error hit me like a ton of bricks. Here’s the scoop:

    {
      "error": {
        "code": 400,
        "message": "vendorTemplate.parameter.measurementId: The value must not be empty.nvendorTemplate.parameter.eventName: The value must not be empty.n",
        "errors": [
          {
            "message": "vendorTemplate.parameter.measurementId: The value must not be empty.nvendorTemplate.parameter.eventName: The value must not be empty.n",
            "domain": "global",
            "reason": "badRequest"
          }
        ],
        "status": "INVALID_ARGUMENT"
      }
    }
    

    Now, I’m no Sherlock, but the documentation (link) doesn’t mention anything about vendorTemplate as a required or optional parameter. However, being the brave soul that I am, I tried adding vendorTemplate as an attribute in the request payload anyways. And guess what it says: Property "vendorTemplate" does not exist in object specification.

    Now I’m feeling a bit stuck. Can’t create a GA4 tag using REST API, which puts my dream of migrating from Universal Tag to GA4 tags on the back burner.

    If anyone out there can lend a hand with creating GA4 tags and migrating Universal Analytics Tags to GA4 tags using REST APIs, it would be a lifesaver! I even tried to solve the vendorTemplate error by adding it to the request payload, to no avail. So, my friends, any hints, tips, or tricks are warmly welcomed — help me make this magic happen!

    Liam replied 1 year, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • 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!

  • Liam

    Member
    29 June 2023 at 3:40 pm

    I’m sorry to hear about the issues you’ve encountered while trying to create GA4 tags through the Google Tag Manager REST APIs. It’s unclear why you’re receiving errors suggesting the vendorTemplate parameter is causing issues; especially when you’re noting it isn’t specified as a necessary attribute in the official API documentation. This suggests there might be a discrepancy between the API’s actual requirements and its outlined documentation.

    That being said, Google Analytics 4 (GA4) is a newer version of Google Analytics and Google might not yet have fully integrated its APIs (like the Tag API) to seamlessly support GA4. While the manual approach might seem cumbersome, it might be the most reliable way to make the transition at this point in time. However, I’d recommend reaching out to Google Support directly or posting this question on a Google Developer forum for a more definite answer from officials or fellow developers who might’ve faced and solved this exact issue.

Log in to reply.