Google Ads Script: How to link an asset to an ad group?

84 views
Skip to first unread message

Jan Gramm

unread,
Jun 11, 2025, 8:00:02 AM6/11/25
to Google Ads Scripts Forum

Hi everyone,

I'm working on a Google Ads Script to automatically add an image asset from Google Drive to a specific ad group.

My script is almost working perfectly:

  1. It correctly finds the target campaign and ad group.
  2. It successfully fetches the image from Google Drive.
  3. It successfully creates the image asset in the Google Ads account (Successfully created asset).

However, I'm stuck on the final step: linking this newly created asset to the ad group.

The line adGroup.addAsset("IMAGE", asset); in my code fails and produces the following error in the logs: TypeError: adGroup.addAsset is not a function

What's interesting is that I can manually link the newly created asset to the desired ad group using the Google Ads interface (in the Assets library). This suggests that the issue is not with the asset or the ad group, but with the specific function I'm using in the script. The adGroup.addAsset() method seems to be either deprecated or simply incorrect.

My question is: What is the correct method or code snippet to link an existing asset to an AdGroup using a script?

Below is my full script and the corresponding logs that show the process up to the point of failure.

Thanks in advance for your help!


My Script:
// Enable the Drive API in the Google Ads Scripts editor under "Services".

function main() {
  // --- CONFIGURATION ---
  // 1. Paste the ID of the image file from your Google Drive.
  //    To get the ID, open the image in Drive, and copy the string of characters
  //    from the URL after "file/d/".
  const GOOGLE_DRIVE_IMAGE_ID = "1k6FLdDsPGpMNHk4BfEUDexm3itqsEuuh";

  // 2. Specify the exact name of the campaign containing the ad group.
  const CAMPAIGN_NAME = "ALT | Search Traffic 2025";

  // 3. Specify the exact name of the ad group to link the asset to.
  const AD_GROUP_NAME = "OMEGA";
  // --- END CONFIGURATION ---


  // Find the target ad group.
  console.log(`Searching for ad group "${AD_GROUP_NAME}" in campaign "${CAMPAIGN_NAME}"...`);
  const adGroupIterator = AdsApp.adGroups()
    .withCondition(`campaign.name = "${CAMPAIGN_NAME}"`)
    .withCondition(`ad_group.name = "${AD_GROUP_NAME}"`)
    .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`Could not find ad group named "${AD_GROUP_NAME}" in campaign "${CAMPAIGN_NAME}". Please check names and try again.`);
  }
  const adGroup = adGroupIterator.next();
  console.log(`✔️ Found Ad Group: "${adGroup.getName()}" (ID: ${adGroup.getId()})`);

  // Fetch the image from Google Drive.
  console.log(`Fetching image from Google Drive (ID: ${GOOGLE_DRIVE_IMAGE_ID})...`);
  let imageBlob;
  try {
    imageBlob = DriveApp.getFileById(GOOGLE_DRIVE_IMAGE_ID).getBlob();
  } catch (e) {
    throw new Error(`Failed to fetch file from Google Drive. Make sure the ID is correct and you have permission to access it. Error: ${e}`);
  }
   console.log(`✔️ Fetched image data successfully.`);

  // Create a unique name for the new asset to avoid conflicts.
  const assetName = `${AD_GROUP_NAME} Image Asset ${new Date().getTime()}`;

  // Build the image asset in Google Ads.
  console.log(`Creating image asset with name: "${assetName}"...`);
 
  const assetOperation = AdsApp.adAssets().newImageAssetBuilder()
    .withName(assetName)
    .withData(imageBlob)
    .build();

  if (!assetOperation.isSuccessful()) {
    throw new Error(`Could not create the image asset. Errors: ${assetOperation.getErrors().join(', ')}`);
  }
  const asset = assetOperation.getResult();
  console.log(`✔️ Successfully created asset (ID: ${asset.getId()})`);

  // Link the new asset to the ad group.
  console.log(`Linking asset to ad group "${adGroup.getName()}"...`);
  adGroup.addAsset("IMAGE", asset); // <-- THIS LINE CAUSES THE ERROR
  console.log(`✅ Success! Asset "${asset.getName()}" is now linked to ad group "${adGroup.getName()}".`);
}

Google Ads Scripts Forum

unread,
Jun 11, 2025, 7:12:57 PM6/11/25
to Google Ads Scripts Forum

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

I would like to inform you that there are no standard methods in the Google Ads Scripts to add assets to an ad group. You may refer to the AdsApp.​AdGroup, AdsApp.​AdAssets, and AdsApp.​Asset documents to view all the available methods.

I hope this helps! Feel free to get back to us if you have any other concerns related to the scripts.

Thanks,
Google Ads Scripts team

Reply all
Reply to author
Forward
0 new messages