Automation: Set up Google Analytics for New Site

Learn How to Let your AI Agent set up Google Analytics for a New Website (via GA API)

Slowly but surely my AI Directory Maker Swarm is taking shape. Though there are issues with Google Analytics, I still use it by default on each new site. This post shares how I automated adding Google Analytics to new websites, ultimately via API.

At first I tried adding new sites via AI browser automations, (for fun?), but in the end the API method is much more reliable, and super easy to set up.

Giving your AI Agent Google Analytics Powers

Where does my Google Analytics bot Sit in AI Directory Maker?

The first ProfitSwarm experiment is to make a swarm of AI agents which think up, deploy, and market a directory website.

This agent, (GA bot), was originally one of the last steps needed to complete the “Build Directory” workflow. I quickly realised it just makes more sense to move it to early on in the Directory Builder flow. It now sits just after “register domain”:

Automation Workflow: AI Directory Builder Agent (v0.2)

Automating New Website Connectivity

There are several services that I like to connect to each new website I make; it’s become a kind of “Woodys Boilerplate” over the years:

  • Google Analytics
  • Google Search Console
  • CloudFlare Turnstile (for forms)
  • SE Ranking (or Ahrefs)
  • URL Monitor (or another index rusher tool)

This set of tools has served me well in building out various production sites.

So obviously I need robots to be able to deal with the particulars.

New here? Welcome! This is the journey of building a 100% automated AI business in 2025. You’re jumping in after we’ve already kicked things off, so you might want to catch up first.

Check out these key posts to get the full story—and don’t forget to subscribe for updates and exclusive perks:

How I Automated Adding Google Analytics to a New Site

This is super simple with Google’s Data API. Here are the steps needed to get this working (code below):

Step 1. Set up API Access with Google

Go ahead and load https://console.cloud.google.com/apis/dashboard, then walk through the following steps to generate your JWT key:

  1. Make a new project in Google Cloud Console
  2. Enable the API: ‘Google Analytics Admin API’
  3. Enable the API: ‘Google Search Console API’
  4. Create a new service account here and copy the email it generates
  5. Grant ‘Editor’ access in service account creation flow
  6. Manage keys (on new account record) -> Add key -> Create new key
  7. Select JSON type, and download the key
  8. Lastly, grant the service account the ‘Editor’ or ‘GA Admin’ role in your Google Analytics account (In Google Analytics: Admin > Account Access Management > Add user, then enter the service account’s email from step 4)
Generating a JWT token in Google Data API for Google Analytics Access
Generating a JWT token in Google Data API for Google Analytics Access

Step 2. Call the API via code

That’s the hard part done! Next I wrote code to call the Google Data API and add the new site to GA.

This requires a few steps, as follows:

  1. Get Service Access Token (use your JWT token from above to get a usable access token)
  2. Check the site isn’t already added to your GA account
  3. Add new site as a GA Property
  4. Add new data stream to new GA Property
  5. Save the ‘data-measurement-id’ to be able to inject it into the site JS so that Google Analytics works

You can see the full code I used below, but it’s not rocket science if you’re used to calling APIs.

Once you’ve got this logic wired into your Business API, you can use any AI workflow tool (like Gumloop or Lindy) to build an AI Agent which automatically connects your website to Google Analytics for stats.

Connect Google Analytics via API: The Code

If you’re already a subscriber you’ll see that the Business API Boilerplate already has this code setup with an endpoint – you can literally just call the API. For those who’re reading this before joining ProfitSwarm, you can see the simple version here in the public repo.

Script to add Google Analytics to your new site

Aside: Using AI Browser Agent to setup Google Analytics

When I first approached this I was hankering to play with browser-use (AI controlled browser agent), so I had a go writing a python script for it to walk through the Google Analytics AI and add a new website.

This, like so many AI tools you’ve read about here, almost worked.

I probably could have got it working, but I realised I was wasting time when I could just do this via API.

It was good to get familiar with browser-use though, I did manage to:

  • Get it using my default Chrome browser (instead of chromium)
  • Get it walking through all of the steps in the Google UI

I will be doing a lot more with AI browser control in the coming weeks.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *