Tutorial: Setting Up Automatic Ad tracking for HubSpot in Google Ads

Tutorial: Setting Up Automatic Ad tracking for HubSpot in Google Ads

This tutorial will guide you through setting up Ad tracking for HubSpot in Google Ads, using a script that allows for auto-tagging of your campaigns and ad groups. This setup can help you monitor and optimize your advertising efforts. Here’s how you can accomplish this:

Step 1: Create a Google Ads Script

Below is the script that you will be using

Script Name: Donutz Campaign & AdGroup tagging

function main() {

var adGroupIterator = AdsApp.adGroups().withCondition("CampaignStatus = ENABLED").get();

while (adGroupIterator.hasNext()) {

var adGroup =;

var gn = adGroup.getName().replace(/\\\\s/g, '_');

var cn = adGroup.getCampaign().getName().replace(/\\\\s/g, '_');

adGroup.urls().setCustomParameters({adgroup: gn, campaign: cn});



This script iterates through all your active ad groups, collects their names and their respective campaign names, and assigns them as custom parameters.

Step 2: Schedule the Google ads Script

Ensure that you schedule this script to run daily. This will ensure that any new ad groups added are automatically updated.

Step 3: Set Up the Tracking Template (Ad Tracking For Hubspot) in Google Ads

Go to the tracking template at the account level. Here, you need to define two variables: _campaign and _adgroup. These are the variables you see in the script. The underscore before the variable name is essential.

Tutorial: Setting Up Automatic Ad tracking for HubSpot in Google Ads image 9

The tracking template should look similar to this:

{lpurl}?utm_source=google&&utm_medium=cpc&utm_campaign={**{_campaign}**}&utm_adgroup={**{_adgroup}**}utm_term={keyword}&&hsa_acc=**1234(to be replaced by hubspot ID)**&hsa_cam=campaignid&hsa_grp={adgroupid}&hsa_ad={creative}&hsa_src={network}&hsa_tgt={targetid}&hsa_kw={keyword}&hsa_mt={matchtype}&hsa_net=google_ads&hsa_ver=3

For the HubSpot variables, they should point to Google’s ValueTrack parameters campaignid, adgroupid, etc. Since HubSpot is connected to Google Ads API, it can retrieve the campaign name using this ID.

💡 To find more about valuetrack visit this page :

💡 Hubspot documentation :

Step 4: Run the Google Ads Script

Once you’ve set up the tracking template correctly, run the script to run daily.

Step 5: Verify Your Changes

Check that your campaigns have been updated at the ad group level with the custom parameters you’ve set.


Make sure to avoid overriding or altering the configuration at the account level. Also, remove any configurations at the campaign level or adgroup and keyword level to ensure higher-level configurations take precedence.

By following these steps, you’ll have a working tracking template in Google Ads.


MJ is the founder of Donutz. He is an expert in developing innovative strategies and implementing various tools.

Follow our newsletter

Latest Articles