I'm working on a Google Ads Script to automatically add an image asset from Google Drive to a specific ad group.
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.
Below is my full script and the corresponding logs that show the process up to the point of failure.
// 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()}".`);
}