Maximize the performance of your PMax campaigns with Google Ads scripts

Although Performance Max (PMax) helps advertisers create effective campaigns automatically, advertisers may still want to maintain control over certain aspects of their campaigns. Setting up scripts in your account is a great way to manage and control your campaigns. Finally, on September 13, 2022, Google announced it added support for PMax campaigns in Ads Scripts.

You can perform common operations like pausing or activating the campaign with scripts, as well as selecting and changing most asset types in asset groups. By using Ads Scripts, advertisers can maintain control over their campaigns while taking advantage of PMax’s automation.

In this article, we have explained each script in detail about how Google Ads Scripts works for PMax campaigns.

On this post
    Maximize the performance of your PMax campaigns with Google Ads scripts

    Useful Google Ads scripts for PMax campaigns

    Retrieve all performance max campaigns:

    This script retrieves all PMax campaigns using the AdsApp.performanceMaxCampaigns() function in Google Ads Scripts. The function returns an iterator containing all campaigns that aren’t removed by default. The script then logs the total number of campaigns found and returns the iterator.

    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;
    }
    

    Retrieve a performance max campaign by its name:

    This script is used to retrieve information about a specific PMax campaign in Google Ads by name. The getPerformanceMaxCampaignByName() function takes a campaign name as input and uses AdsApp.performanceMaxCampaigns().withCondition() to filter the campaigns and return the one that matches the specified name. If the campaign isn’t found, the script prints an error message stating that no campaign with the specified name was found. If the campaign is found, the script logs various details about the campaign, such as name, status, bidding strategy, ad rotation type, start date, and end date. The information is logged using the console.log() function. The script also returns the PMax campaign object. The formatDate() function is used to format the start and end dates of the 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);
    }
    

    Retrieve a performance max campaign’s stats:

    This script retrieves statistics for a specific PMax campaign in Google Ads by name. The getPerformanceMaxCampaignStats() function takes a campaign name as input and uses AdsApp.performanceMaxCampaigns().withCondition() to filter the campaigns and return the one that matches the specified name. If the campaign isn’t found, the script prints an error message stating that no campaign with the specified name was found. If the campaign is found, the script retrieves the statistics for the last month using the getStatsFor() function. The function returns the statistics, including the number of impressions and views, and the script logs this information to the console using the console.log() function. Then, the script returns the statistics object.

    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;
    }
    

    Pause a performance max campaign:

    This script pauses a specific PMax campaign in Google Ads by name. The pausePerformanceMaxCampaign() function takes a campaign name as input and uses AdsApp.performanceMaxCampaigns().withCondition() to filter the campaigns and return the one that matches the specified name. If the campaign is found, the script pauses the campaign using the pause() function. If the campaign isn’t found, no action is taken and the script continues to run.

    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:

    This script retrieves an asset group within a given PMax campaign in Google Ads by name. The getAssetGroupByName() function takes two arguments: the name of the PMax campaign and the name of the asset group. It first uses the getPerformanceMaxCampaignByName() function to retrieve the specified PMax campaign. When the campaign is found, the function continues and uses the assetGroups().withCondition() method to retrieve the asset group within the campaign and filters the asset groups based on the specified name. If the asset group is found, the function returns it. Otherwise, an error is returned stating that no asset group with the specified name was found.

    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:

    This script is a function that allows you to pause a PMax asset group in a Google Ads campaign, given the name of the campaign and the name of the asset group. The function first calls another function, getAssetGroupByName, to retrieve the specified asset group based on the specified campaign name and asset group name. Once the asset group is retrieved, the function pauses it by calling the pause() method and outputs the paused status of the asset group by checking the result of the isPaused() method.

    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:

    This script is used to get a specific YouTube video from Google Ads by its YouTube video ID. The script uses the AdsApp.adAssets().assets() method to filter the videos in the Google Ads account and finds the specific video with the specified YouTube video ID. If the video is found, it returns the video object, otherwise it returns null.

    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:

    This script adds a YouTube video with a specific YouTube video ID to an asset group in a Google Ads campaign. The asset group is specified by its name and the name of the campaign. The function first calls the getVideoByYouTubeId function to retrieve the video by its YouTube video ID, and then calls the getAssetGroupByName function to retrieve the specified asset group by its name and campaign name. Finally, the function adds the video to the 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:

    This script removes a video from an asset group in a Google Ads PMax campaign. The script starts by calling the getVideoByYouTubeId function, which takes a YouTube video ID as an argument and returns the video asset object associated with that ID. Next, the script calls the getAssetGroupByName function, which takes the campaign name and asset group name as arguments and returns the asset group object. Finally, the script calls the removeAsset method on the asset group object, passing the video asset object and the type of the asset (YOUTUBE_VIDEO) as arguments.

    function removeVideoFromAssetGroup(youTubeVideoId, campaignName, assetGroupName) {
      // Defined above
      const video = getVideoByYouTubeId(youTubeVideoId);
      const assetGroup = getAssetGroupByName(campaignName, assetGroupName);
      assetGroup.removeAsset(video, 'YOUTUBE_VIDEO');
    }
    

    Trouble to set up your own solution?

    Don’t have a developer in your marketing team and having trouble setting up the solution? We can set up and maintain Google Ads Scripts for you. If you’re interested, feel free to contact us. We also have a lot of other solutions for automating things in Google Ads.

    More Similar Posts

    Menu