Performance Max
Stay organized with collections
Save and categorize content based on your preferences.
function getAllPerformanceMaxCampaigns() {
// AdsApp.performanceMaxCampaigns() will return all campaigns that are not
// removed by default.
const performanceMaxCampaignIterator = AdsApp.performanceMaxCampaigns().get();
console.log(`Total campaigns found : ${performanceMaxCampaignIterator.totalNumEntities()}`);
return performanceMaxCampaignIterator;
}
Usage
const performanceMaxCampaigns = getAllPerformanceMaxCampaigns();
for (const performanceMaxCampaign of performanceMaxCampaigns) {
// Process your campaign.
}
function getPerformanceMaxCampaignByName(campaignName) {
const performanceMaxCampaignIterator = AdsApp.performanceMaxCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
if (!performanceMaxCampaignIterator.hasNext()) {
throw new Error(`No performance max campaign with name ${campaignName} found.`);
}
const performanceMaxCampaign = performanceMaxCampaignIterator.next();
console.log(`Campaign Name: ${performanceMaxCampaign.getName()}`);
console.log(`Enabled: ${performanceMaxCampaign.isEnabled()}`);
console.log(`Bidding strategy: ${performanceMaxCampaign.getBiddingStrategyType()}`);
console.log(`Ad rotation: ${performanceMaxCampaign.getAdRotationType()}`);
console.log(`Start date: ${formatDate(performanceMaxCampaign.getStartDate())}`);
console.log(`End date: ${formatDate(performanceMaxCampaign.getEndDate())}`);
return performanceMaxCampaign;
}
function formatDate(date) {
function zeroPad(number) { return Utilities.formatString('%02d', number); }
return (date == null) ? 'None' : zeroPad(date.year) + zeroPad(date.month) +
zeroPad(date.day);
}
function getPerformanceMaxCampaignStats(campaignName) {
const performanceMaxCampaignIterator = AdsApp.performanceMaxCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
if (!performanceMaxCampaignIterator.hasNext()) {
throw new Error(`No performance max campaign with name ${campaignName} found.`);
}
const performanceMaxCampaign = performanceMaxCampaignIterator.next();
// Fetch stats for the last month. See the DateRangeLiteral section at
// https://developers.google.com/adwords/api/docs/guides/awql#formal_grammar
// for a list of all supported pre-defined date ranges.
// Note: Reports can also be used to fetch stats. See
// https://developers.google.com/google-ads/scripts/docs/features/reports
// for more information.
var stats = performanceMaxCampaign.getStatsFor('LAST_MONTH');
console.log(`${performanceMaxCampaign.getName()}, ${stats.getImpressions()} impressions, ` +
`${stats.getViews()} views`);
return stats;
}
function pausePerformanceMaxCampaign(campaignName) {
const performanceMaxCampaignIterator = AdsApp.performanceMaxCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
if (performanceMaxCampaignIterator.hasNext()) {
const performanceMaxCampaign = performanceMaxCampaignIterator.next();
performanceMaxCampaign.pause();
}
}
Retrieve an asset group by its name
function getAssetGroupByName(campaignName, assetGroupName) {
// Defined above
const performanceMaxCampaign = getPerformanceMaxCampaignByName(campaignName);
if (performanceMaxCampaign == null) {
return null;
}
const assetGroupIterator = performanceMaxCampaign.assetGroups()
.withCondition(`asset_group.name = "${assetGroupName}"`)
.get();
if (!assetGroupIterator.hasNext()) {
throw new Error(`No asset group found with name ${assetGroupName}.`);
}
return assetGroupIterator.next();
}
Pause an asset group
function pausePerformanceMaxAssetGroup(campaignName, assetGroupName) {
// Defined above
const assetGroup = getAssetGroupByName(campaignName, assetGroupName);
assetGroup.pause();
console.log(`AssetGroup with name: ${assetGroup.getName()} ` +
`has paused status: ${assetGroup.isPaused()}`);
}
Retrieve a specific video for use in an asset group
function getVideoByYouTubeId(youTubeVideoId) {
// You can filter on the YouTubeVideoId if you already have that video in
// your account to fetch the exact one you want right away.
const videos = AdsApp.adAssets().assets()
.withCondition(`asset.type = YOUTUBE_VIDEO AND ` +
`asset.youtube_video_asset.youtube_video_id = '${youTubeVideoId}'`)
.get();
if (videos.hasNext()) {
return videos.next();
}
return null;
}
Add a specific video to an asset group
function addVideoToAssetGroup(youTubeVideoId, campaignName, assetGroupName) {
// Defined above
const video = getVideoByYouTubeId(youTubeVideoId);
const assetGroup = getAssetGroupByName(campaignName, assetGroupName);
assetGroup.addAsset(video, 'YOUTUBE_VIDEO');
}
Remove a specific video from an asset group
function removeVideoFromAssetGroup(youTubeVideoId, campaignName, assetGroupName) {
// Defined above
const video = getVideoByYouTubeId(youTubeVideoId);
const assetGroup = getAssetGroupByName(campaignName, assetGroupName);
assetGroup.removeAsset(video, 'YOUTUBE_VIDEO');
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-09-12 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-09-12 UTC."],[[["This script provides functions to manage Google Ads Performance Max campaigns, including retrieving, pausing, and getting stats for campaigns and asset groups."],["You can retrieve campaigns by name or iterate through all of them, and access campaign details like bidding strategy and start/end dates."],["Asset group management includes retrieving by name, pausing, and adding or removing specific videos identified by their YouTube IDs."],["Functions to get campaign and asset group stats are included, utilizing predefined date ranges or enabling the use of reports for more customized analysis."],["Error handling is incorporated to identify and report when campaigns or asset groups with specified names are not found."]]],[]]