AdSense Service

The AdSense service allows you to use the AdSense Management API in Apps Script. This API gives AdSense customers the ability to get information about the structure of their account and run reports on how it is performing.


For detailed information on this service, see the reference documentation for the AdSense Management API. Like all advanced services in Apps Script, the AdSense service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below uses version 1.3 of the API.

List ad clients

This sample lists all of the ad clients within the user's default account. Notice the use of page tokens to access the full list of results.

function listAdClients() {
  // Retrieve ad client list in pages and log data as we receive it.
  var pageToken, adClients;
  do {
    adClients = AdSense.Adclients.list({
      maxResults: 50,
      pageToken: pageToken
    if (adClients.items) {
      for (var i = 0; i < adClients.items.length; i++) {
        var adClient = adClients.items[i];
        Logger.log('Ad client for product "%s" with ID "%s" was found.',
        Logger.log('Supports reporting: %s',
            adClient.supportsReporting ? 'Yes' : 'No');
    } else {
      Logger.log('No ad clients found.');
    pageToken = adClients.nextPageToken;
  } while (pageToken);

List ad units

Listing ad units is very similar to listing ad clients but requires an ad client ID, which can be obtained from the previous sample.

function listAdUnits(adClientId) {
  var pageToken, adUnits;
  do {
    adUnits = AdSense.Adunits.list(adClientId, {
      maxResults: 50,
      pageToken: pageToken
    if (adUnits.items) {
      for (var i = 0; i < adUnits.items.length; i++) {
        var unit = adUnits.items[i];
        Logger.log('Ad unit with code "%s" and name "%s" was found.',
    } else {
      Logger.log('No ad units found.');

    pageToken = adUnits.nextPageToken;
  } while (pageToken);

Generate a report

This sample generates a report over your AdSense account and outputs the results to a spreadsheet.

function generateReport(adClientId) {
  // Prepare report.
  var today = new Date();
  var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);

  var timezone = Session.getTimeZone();
  var startDate = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
  var endDate = Utilities.formatDate(today, timezone, 'yyyy-MM-dd');

  var report = AdSense.Reports.generate(startDate, endDate, {
    // Specify the desired ad client using a filter.
    filter: ['AD_CLIENT_ID==' + escapeFilterParameter(adClientId)],
    dimension: ['DATE'],
    // Sort by ascending date.
    sort: ['+DATE']

  if (report.rows) {
    var spreadsheet = SpreadsheetApp.create('AdSense Report');
    var sheet = spreadsheet.getActiveSheet();

    // Append the headers.
    var headers = {

    // Append the results.
    sheet.getRange(2, 1, report.rows.length, headers.length)

    Logger.log('Report spreadsheet created: %s',
  } else {
    Logger.log('No rows returned.');

 * Escape special characters for a parameter being used in a filter.
 * @param parameter the parameter to be escaped.
 * @return the escaped parameter.
function escapeFilterParameter(parameter) {
  return parameter.replace('\\', '\\\\').replace(',', '\\,');

Send feedback about...

Apps Script
Apps Script