0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[GAS]GoogleAdSenseAPI v2 スプレッドシート記入スクリプト

Posted at

##背景

AdSenseAPIv1.4が2021年10月12日付で終了するのでそれに伴ってGASを書き直さなければならない

##スクリプト

v1.4

function writeAdsenceReport() {
  var spreadSheet = SpreadsheetApp.getActive();
  var sheet = spreadSheet.getSheetByName("raw(AdSence)");
  var adClientId = 'ca-pub-xxxxx';
  var startDate = Moment.moment(spreadSheet.getSheetByName("Variables").getRange(1, 2).getValue()).add(1, 'days').format('YYYY-MM-DD');
  var endDate = Moment.moment(spreadSheet.getSheetByName("Variables").getRange(2, 2).getValue()).add(1, 'days').format('YYYY-MM-DD');
  
  var metrics = ['PAGE_VIEWS', 'INDIVIDUAL_AD_IMPRESSIONS', 'CLICKS', 'PAGE_VIEWS_RPM', 'INDIVIDUAL_AD_IMPRESSIONS_RPM', 'EARNINGS']
  var args = {
    'metric': metrics,
    'dimension': ['DATE'],
    'filter': ['AD_CLIENT_ID==' + adClientId, 'DOMAIN_NAME==tsukulink.net'],
    'useTimezoneReporting': true,
  };
  var report = AdSense.Reports.generate(startDate, endDate, args).getRows();
  sheet.getRange(sheet.getLastRow()+1, 1, report.length, metrics.length + 1).setValues(report);
}
v2

function writeAdsenceReport() {
  var spreadSheet = SpreadsheetApp.getActive();
  var sheet = spreadSheet.getSheetByName("raw(AdSence)");
  var accountName = 'accounts/pub-xxxxx';

  
  var metrics = ['PAGE_VIEWS', 'INDIVIDUAL_AD_IMPRESSIONS', 'CLICKS', 'PAGE_VIEWS_RPM', 'INDIVIDUAL_AD_IMPRESSIONS_RPM', 'ESTIMATED_EARNINGS']
  var report = AdSense.Accounts.Reports.generate(accountName, {
    filters: ['DOMAIN_NAME==tsukulink.net'],
    metrics:  metrics,
    dimensions: ['DATE'],
    dateRange: "LAST_7_DAYS",
    orderBy: ['+DATE']
  })

  if (report.rows) {
      // Append the results.
      sheet.getRange(sheet.getLastRow()+1, 1, report.rows.length, report.headers.length)
        .setValues(report.rows.map(row => row.cells.map(cell => cell.value)));

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

#主な変更点

###ClientID

https://developers.google.com/adsense/management/release_notes
リリースノートより

All deprecated methods in v1.4 have been removed. This includes resource methods that did not require an accountId. In v2, the accountId is specified in the parent field.
Per Google API standards, resources are now identified by a name field. For example, an AdClient’s name would look like accounts/{accountId}/adclients/{adClientId}. Additionally, in v1.4 the resource ID was used as a reporting dimension, but in v2 that value is now available through a reporting_dimension_id field on several resources.

上記のような変更点があったみたいですが、なんのことかあまり理解できていないです(わかる方いたらご教授いただきたいです)

とりあえずv2ではIDではなく名前で識別するっぽいのでaccounts/pub-数字にすれば良さそう。

###クエリパラメーターの命名が一部複数形に
metrics, dimensionsなどのフィールド名が複数形に変更されている。細かい部分で気づきにくいので注意。

###reportの出力形式がJSONに
今まではreportの出力形式は二次元配列だったのですが、v2ではJSONになっています。

ただし、
report.rows.map(row => row.cells.map(cell => cell.value))
こちらを実行していただければ今まで通り二次元配列で出力してくれます。

##終わり
他にも細かい変更点ありますがざっ影響与えそうな変更点だけ挙げました。
参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?