Search code examples
continuous-integrationgoogle-tag-managercicd

GTM control on CI/CD


As a Software engineer I would like to keep multiple website environments having different GTM accounts consistent.

Is there an article from google explaining how to use GTM APIs to perform CI/CD import / exports.

If this is not supported by GTM does anyone know of a way to achieve that?


Solution

  • Integration with CI/CD is not natively supported, but GTM has an API that could be leveraged for exporting/importing GTM containers.

    Use this to get a version.

    And then... I believe this endpoint is for updating the version with the one you've downloaded.

    An update: I looked into the update endoint... It looks like it's not gonna be easy to utilize it for quick bulk upload. It seems like GTM neglected to make their existing workspace upload functionality available via api. Wow. Yep, confirmed. Looks like it's just not there. Well, Google is far from perfect.

    You, theoretically, could use... A selenium/puppeteer bot to simulate doing it manually. Or even just investigate what GTM's frontend sends to the backend when you upload the version through container input and reimplement it as a part of your CI. However, you probably don't want anything like it there. There's this super helpful repo that allows you to basically cast your version get api response into an uploadable json, but you can't upload it via their public api. Here's the repo: https://github.com/sahava/gtm-api-to-export-json


    On the other hand.

    FYI, in vast majority of cases, CI/CD should not affect GTM since GTM is meant to be handled by analytics rather than the devs. Therefore, it's unconventional to have CI/CD syncing the containers. Analytics typically wouldn't need syncing containers across different site environments when they have proper GTM architecture. And the proper (best practices) GTM containers set up is to have prod and lowers using the same container. The container has the ability to read all global JS context, so determining whether it's running in prod or lowers is almost always trivial.

    In some rare cases, it's not feasible to have the same gtm container in prod and lowers. For that, GTM has GTM environments. They make syncing inbetween envs very simple, again, not requiring CI/CD involvement.