勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

GASとAdSense Management APIを使って前日分の売り上げをメール通知する方法

   2024/02/06  Google Apps Script Google

Googleアドセンスの売り上げをチェックする際、いつもはアドセンスの管理画面に見に行っていた。これが面倒臭いのでGoogle Apps ScriptとAdSense Management APIを組み合わせて昨日分のGoogleアドセンスの売り上げをメールで受け取りたい。以下に実装方法をメモ。

 

AdSense Management APIについて

リファレンス等は以下公式サイトより確認できる。

https://developers.google.com/adsense/management?hl=ja

 

実装方法

AdSense Management APIの準備

Googleスプレッドシートを開き「拡張機能→Apps Script」をクリックする。ページ左側の「サービス+」をクリックし「AdSense Management API」を追加する。正常に追加されると以下のように表示される筈。

アドセンスIDの準備

Googleアドセンスの管理画面を開く際に以下のようなURLになるので【アドセンスID】部分をメモしておく。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
https://www.google.com/adsense/new/u/0/pub-【アドセンスID】/home
https://www.google.com/adsense/new/u/0/pub-【アドセンスID】/home
https://www.google.com/adsense/new/u/0/pub-【アドセンスID】/home

ソースコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function getAdSenseData(){
//アドセンスID設定
const adsense_id = `xxxxxxxxxxxxx`;
const adsense_account = `accounts/pub-${adsense_id}`;
const adsense_dimension_id = `ca-pub-${adsense_id}`;
//昨日のデータを指定
const date = new Date();
date.setDate(date.getDate() - 1);
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
const ymd = `${y}-${m}-${d}`;
//レポートの取得
const report = AdSense.Accounts.Reports.generate(adsense_account, {
filters: [`AD_CLIENT_ID==${adsense_dimension_id}`],
metrics: [`ESTIMATED_EARNINGS`],
dimensions: [`DATE`],
'startDate.day': d,
'startDate.month': m,
'startDate.year': y,
'endDate.day': d,
'endDate.month': m,
'endDate.year': y,
});
if( report[`totals`][`cells`][1][`value`] ){
sendMail( report[`totals`][`cells`][1][`value`], ymd );
}
}
function sendMail( sales, ymd ){
//送信先メールアドレス
const recipient = `xxxxxxxxxxxx@gmail.com`;
//メール件名
const subject = `アドセンス通知`;
const message = `${ymd}の売り上げは${sales}円です。`;
//メール送信
GmailApp.sendEmail(recipient, subject, message)
}
function getAdSenseData(){ //アドセンスID設定 const adsense_id = `xxxxxxxxxxxxx`; const adsense_account = `accounts/pub-${adsense_id}`; const adsense_dimension_id = `ca-pub-${adsense_id}`; //昨日のデータを指定 const date = new Date(); date.setDate(date.getDate() - 1); const y = date.getFullYear(); const m = date.getMonth() + 1; const d = date.getDate(); const ymd = `${y}-${m}-${d}`; //レポートの取得 const report = AdSense.Accounts.Reports.generate(adsense_account, { filters: [`AD_CLIENT_ID==${adsense_dimension_id}`], metrics: [`ESTIMATED_EARNINGS`], dimensions: [`DATE`], 'startDate.day': d, 'startDate.month': m, 'startDate.year': y, 'endDate.day': d, 'endDate.month': m, 'endDate.year': y, }); if( report[`totals`][`cells`][1][`value`] ){ sendMail( report[`totals`][`cells`][1][`value`], ymd ); } } function sendMail( sales, ymd ){ //送信先メールアドレス const recipient = `xxxxxxxxxxxx@gmail.com`; //メール件名 const subject = `アドセンス通知`; const message = `${ymd}の売り上げは${sales}円です。`; //メール送信 GmailApp.sendEmail(recipient, subject, message) }
function getAdSenseData(){

    //アドセンスID設定
    const adsense_id = `xxxxxxxxxxxxx`;
    const adsense_account = `accounts/pub-${adsense_id}`;
    const adsense_dimension_id = `ca-pub-${adsense_id}`;

    //昨日のデータを指定
    const date = new Date();
    date.setDate(date.getDate() - 1);
    const y = date.getFullYear();
    const m = date.getMonth() + 1;
    const d = date.getDate();
    const ymd = `${y}-${m}-${d}`;

    //レポートの取得
    const report = AdSense.Accounts.Reports.generate(adsense_account, {
        filters: [`AD_CLIENT_ID==${adsense_dimension_id}`],
        metrics: [`ESTIMATED_EARNINGS`],
        dimensions: [`DATE`],
        'startDate.day': d,
        'startDate.month': m,
        'startDate.year': y,
        'endDate.day': d,
        'endDate.month': m,
        'endDate.year': y,
    });

    if( report[`totals`][`cells`][1][`value`] ){
        sendMail( report[`totals`][`cells`][1][`value`], ymd );
    }
}

function sendMail( sales, ymd ){

    //送信先メールアドレス
    const recipient = `xxxxxxxxxxxx@gmail.com`;

    //メール件名
    const subject = `アドセンス通知`;

    const message = `${ymd}の売り上げは${sales}円です。`;

    //メール送信
    GmailApp.sendEmail(recipient, subject, message) 

}

アドセンスID部分は先ほどメモしたものを入力する。送信先メールアドレスも適宜変更すること。

尚、初回実行時は権限の確認が必要になるので案内に従って進めること。

トリガー設定

毎日自動実行させるためにトリガー設定を行う必要がある。Apps Scriptページの左側に目覚まし時計っぽいアイコンがあるのでそちらからトリガー設定ページに進み以下を設定する(時刻部分は好きな時間帯でOK)。

 

所感

Google系のサービスと連携したシステムを作る際、PHPだとGoogle Cloud Platformでの設定作業や各種ライブラリのインストール作業が面倒臭い。この辺りを簡潔に実装できるのがGASの強みだなぁと思った。

 - Google Apps Script Google

  関連記事

GASでNintendo Storeをチェックし指定したソフトが〇円以下の際に通知する方法

Google Apps Scriptを利用してNintendo Storeをスク ...

GASとGoogleカレンダを連携し暦通りではない店休日を取得する方法

小売店等のサイトで営業日カレンダーみたいなものを表示させる際に今まではPHPで管 ...

GASを利用してGmailに届いたメール内容をGoogleスプレッドシートに記入する方法

WordPressのContact Form7から送信した内容をGmailで受け ...

GASとLINEを連携させて通知メッセージを送る方法のメモ

Google Apps ScriptとLINEを連携させ、LINEに何らかの通知 ...

GASとGoogleアナリティクスを連携させ昨日分のアクセス数TOP3位のページタイトル及び合計アクセス数を取得する方法

GASとGoogleアナリティクスを連携させて「昨日分のアクセス数TOP3位のペ ...

S