PMax Search Terms

Export Search Terms from your PMax campaigns to Google Spreadsheets

Start Now!
Zoektermen van PMax campagnes inzichtelijk maken
PMax Search Get started!

Finally, it is now possible to export search term data from your PMax campaigns. This allows you to quickly see which search terms your campaign is showing on. Unfortunately no cost data (yet), but it does let you know if you are bringing in relevant impressions or not.

How it works

This script selects all PMax campaigns in your account and exports the data to a Google Spreadsheet. A separate tab is created for each campaign. Without modifications, the script will export the last 30 days of data, but you can customize this to your liking.

Settings

Configure the script by adjusting the following parameters in the CONFIG:

  • LOG: if you want to see more information in the debug of the script, set it to 'true'
  • DATE_RANGE: Adjust only the number to the number of days the script must look back to collect data
  • SPREADSHEET_URL: Create a new spreadsheet, copy and paste the URL here

Frequency: entirely at your discretion. Maximum of 1x per day.

The script
// Copyright 2023
// Free to use or alter for everyone. A mention would be nice ;-)
//
// Idea By: Mike Rhodes
// Put on steroids by: Tibbe van Asten
// 
// Created: 15-08-2023
// Last update: 18-08-2023: fixed a little mistake in the query, that prevents you from collecting more than 30 days data
//
// ABOUT THE SCRIPT
// Export search term from Pmax campaigns to a spreadsheet.
//
////////////////////////////////////////////////////////////////////

var config = {

    LOG : false,

    // Change the number of days of which data must be exported. Only change the number!
    DATE_RANGE: last_n_days(30),
    
    // Edit the URL of an empty Google Sheet in here, with '/edit' at the end
    SPREADSHEET_URL : ""
  
  }  
    
  ////////////////////////////////////////////////////////////////////
  
  function main() {
    
      var spreadsheet = SpreadsheetApp.openByUrl(config.SPREADSHEET_URL);
    
    let campaignIterator = AdsApp
      .performanceMaxCampaigns()
      .withCondition("campaign.status = ENABLED")
      .get();
    
    while(campaignIterator.hasNext()){
      let campaign = campaignIterator.next();
      
      let query = AdsApp.report(
        "SELECT campaign_search_term_insight.category_label, metrics.clicks, metrics.impressions, metrics.conversions, metrics.conversions_value " +
        "FROM campaign_search_term_insight " +
        "WHERE campaign_search_term_insight.campaign_id = '" + campaign.getId() + "' " +
        "AND segments.date BETWEEN '" + config.DATE_RANGE.split(',')[0] + "' AND '" + config.DATE_RANGE.split(',')[1] + "' " +
        "ORDER BY metrics.impressions DESC"
      );
      
        if(config.LOG === true){
           Logger.log("Report " + campaign.getName() + " contains " + query.rows().totalNumEntities() + " rows.");
        }
      
      let sheet = checkTab(spreadsheet, campaign.getName());
      query.exportToSheet(sheet);
    } // campaignIterator    
    
  }
    
  ////////////////////////////////////////////////////////////////////
  
  function checkTab(file, tabName){
    
    if(SpreadsheetApp.openById(file.getId()).getSheetByName(tabName)){
      var tab = SpreadsheetApp.openById(file.getId()).getSheetByName(tabName);
      
      if(config.LOG === true){
       Logger.log("Selected tab " + tabName);      
      }
    } else {
      var tab = SpreadsheetApp.openById(file.getId()).insertSheet(tabName);
      
        if(config.LOG === true){
         Logger.log("Created tab " + tabName);      
        }
    }
    
    // Remove default tab in Dutch
    if(SpreadsheetApp.openById(file.getId()).getSheetByName("Blad1")){
      var defaultSheet = SpreadsheetApp.openById(file.getId()).getSheetByName("Blad1")
      SpreadsheetApp.openById(file.getId()).deleteSheet(defaultSheet);
    }
    
    // Remove default tab in English
    if(SpreadsheetApp.openById(file.getId()).getSheetByName("Sheet1")){
      var defaultSheet = SpreadsheetApp.openById(file.getId()).getSheetByName("Sheet1")
      SpreadsheetApp.openById(file.getId()).deleteSheet(defaultSheet);
    }
    
    return tab;
    
  } // function checkTab
  
  ////////////////////////////////////////////////////////////////////
  
  function last_n_days(n) {
  
      var	from = new Date(), to = new Date();
      to.setUTCDate(from.getUTCDate() - n);
    from.setUTCDate(from.getUTCDate() - 1);
  
      return google_date_range(from, to);
  
  } // function last_n_days()
  
  ////////////////////////////////////////////////////////////////////
  
  function google_date_range(from, to) {
  
      function google_format(date) {
          var date_array = [date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()];
          if (date_array[1] < 10) date_array[1] = '0' + date_array[1];
          if (date_array[2] < 10) date_array[2] = '0' + date_array[2];
  
          return date_array.join('');
      }
  
      var inverse = (from > to);
      from = google_format(from);
      to = google_format(to);
      var result = [from, to];
  
      if (inverse) {
          result = [to, from];
      }
  
      return result.join(',');
  
  } // function google_date_range()
Show whole script!
Loading Comments
The Experts
Tibbe van Asten Team Lead Performance Marketing
Nils Rooijmans Water Cooler Topics
Martijn Kraan Freelance PPC Specialist
Bas Baudoin Teamlead SEA @ Happy Leads
Jermaya Leijen Digital Marketing Strategist
Krzysztof Bycina PPC Specialist from Poland
How about you? JOIN US!
Sharing Knowledge
Caring

Adsscripts.com is all about sharing knowledge. In the current market, PPC specialists like to keep their knowledge and experience to themselves. We're convinced that sharing knowledge can ensure that everyone gets better at their work. We want to change this by sharing our knowledge about scripts with everyone.

Do you also want to contribute? We are open to new ideas and feedback on everything you find on Adsscripts.com.

Contact us

Training &
Workshop
Contact us!
Adsscripts Training & Workshop