13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+ // [START apps_script_adsense_list_accounts]
17
+ /**
18
+ * Lists available AdSense accounts.
19
+ */
20
+ function listAccounts ( ) {
21
+ let pageToken ;
22
+ do {
23
+ const response = AdSense . Accounts . list ( { pageToken : pageToken } ) ;
24
+ if ( response . accounts ) {
25
+ for ( const account of response . accounts ) {
26
+ Logger . log ( 'Found account with resource name "%s" and display name "%s".' ,
27
+ account . name , account . displayName ) ;
28
+ }
29
+ } else {
30
+ Logger . log ( 'No accounts found.' ) ;
31
+ }
32
+ pageToken = response . nextPageToken ;
33
+ } while ( pageToken ) ;
34
+ }
35
+ // [END apps_script_adsense_list_accounts]
36
+
16
37
// [START apps_script_adsense_list_ad_clients]
17
38
/**
18
- * Logs a lists Ad clients.
39
+ * Logs available Ad clients for an account.
40
+ *
41
+ * @param {string } accountName The resource name of the account that owns the collection of ad clients.
19
42
*/
20
- function listAdClients ( ) {
21
- // Retrieve ad client list in pages and log data as we receive it.
22
- var pageToken ;
23
- var adClients ;
43
+ function listAdClients ( accountName ) {
44
+ let pageToken ;
24
45
do {
25
- adClients = AdSense . Adclients . list ( {
26
- maxResults : 50 ,
46
+ const response = AdSense . Accounts . Adclients . list ( accountName , {
27
47
pageToken : pageToken
28
48
} ) ;
29
- if ( adClients . items ) {
30
- for ( var i = 0 ; i < adClients . items . length ; i ++ ) {
31
- var adClient = adClients . items [ i ] ;
32
- Logger . log ( 'Ad client for product "%s" with ID "%s" was found.' ,
33
- adClient . productCode , adClient . id ) ;
34
- Logger . log ( 'Supports reporting: %s' ,
35
- adClient . supportsReporting ? 'Yes' : 'No' ) ;
49
+ if ( response . adClients ) {
50
+ for ( const adClient of response . adClients ) {
51
+ Logger . log ( 'Found ad client for product "%s" with resource name "%s".' ,
52
+ adClient . productCode , adClient . name ) ;
53
+ Logger . log ( 'Reporting dimension ID: %s' ,
54
+ adClient . reportingDimensionId ?? 'None' ) ;
36
55
}
37
56
} else {
38
- Logger . log ( 'No ad clients found.' ) ;
57
+ Logger . log ( 'No ad clients found for this account .' ) ;
39
58
}
40
- pageToken = adClients . nextPageToken ;
59
+ pageToken = response . nextPageToken ;
41
60
} while ( pageToken ) ;
42
61
}
43
62
// [END apps_script_adsense_list_ad_clients]
44
63
45
64
// [START apps_script_adsense_list_ad_units]
46
65
/**
47
66
* Lists ad units.
48
- * @param {string } adClientId The ad client ID .
67
+ * @param {string } adClientName The resource name of the ad client that owns the collection of ad units .
49
68
*/
50
- function listAdUnits ( adClientId ) {
51
- var pageToken ;
52
- var adUnits ;
69
+ function listAdUnits ( adClientName ) {
70
+ let pageToken ;
53
71
do {
54
- adUnits = AdSense . Adunits . list ( adClientId , {
55
- maxResults : 50 ,
72
+ const response = AdSense . Accounts . Adclients . Adunits . list ( adClientName , {
73
+ pageSize : 50 ,
56
74
pageToken : pageToken
57
75
} ) ;
58
- if ( adUnits . items ) {
59
- for ( var i = 0 ; i < adUnits . items . length ; i ++ ) {
60
- var unit = adUnits . items [ i ] ;
61
- Logger . log ( 'Ad unit with code "%s" and name "%s" was found.' ,
62
- unit . code , unit . name ) ;
76
+ if ( response . adUnits ) {
77
+ for ( const adUnit of response . adUnits ) {
78
+ Logger . log ( 'Found ad unit with resource name "%s" and display name "%s".' ,
79
+ adUnit . name , adUnit . displayName ) ;
63
80
}
64
81
} else {
65
- Logger . log ( 'No ad units found.' ) ;
82
+ Logger . log ( 'No ad units found for this ad client .' ) ;
66
83
}
67
84
68
- pageToken = adUnits . nextPageToken ;
85
+ pageToken = response . nextPageToken ;
69
86
} while ( pageToken ) ;
70
87
}
71
88
// [END apps_script_adsense_list_ad_units]
72
89
73
90
// [START apps_script_adsense_generate_report]
74
91
/**
75
- * Generates a spreadsheet report for an ad client.
76
- * @param {string } adClientId The ad client ID
92
+ * Generates a spreadsheet report for a specific ad client in an account.
93
+ * @param {string } accountName The resource name of the account.
94
+ * @param {string } adClientName The reporting dimension ID of the ad client.
77
95
*/
78
- function generateReport ( adClientId ) {
96
+ function generateReport ( accountName , adClientReportingDimensionId ) {
79
97
// Prepare report.
80
- var today = new Date ( ) ;
81
- var oneWeekAgo = new Date ( today . getTime ( ) - 7 * 24 * 60 * 60 * 1000 ) ;
82
-
83
- var timezone = Session . getTimeZone ( ) ;
84
- var startDate = Utilities . formatDate ( oneWeekAgo , timezone , 'yyyy-MM-dd' ) ;
85
- var endDate = Utilities . formatDate ( today , timezone , 'yyyy-MM-dd' ) ;
98
+ const today = new Date ( ) ;
99
+ const oneWeekAgo = new Date ( today . getTime ( ) - 7 * 24 * 60 * 60 * 1000 ) ;
86
100
87
- var report = AdSense . Reports . generate ( startDate , endDate , {
101
+ const report = AdSense . Accounts . Reports . generate ( accountName , {
88
102
// Specify the desired ad client using a filter.
89
- filter : [ 'AD_CLIENT_ID==' + escapeFilterParameter ( adClientId ) ] ,
90
- metric : [ 'PAGE_VIEWS' , 'AD_REQUESTS' , 'AD_REQUESTS_COVERAGE' , 'CLICKS' ,
91
- 'AD_REQUESTS_CTR' , 'COST_PER_CLICK' , 'AD_REQUESTS_RPM' ,
92
- 'EARNINGS' ] ,
93
- dimension : [ 'DATE' ] ,
103
+ filters : [ 'AD_CLIENT_ID==' + escapeFilterParameter ( adClientReportingDimensionId ) ] ,
104
+ metrics : [ 'PAGE_VIEWS' , 'AD_REQUESTS' , 'AD_REQUESTS_COVERAGE' , 'CLICKS' ,
105
+ 'AD_REQUESTS_CTR' , 'COST_PER_CLICK' , 'AD_REQUESTS_RPM' ,
106
+ 'ESTIMATED_EARNINGS' ] ,
107
+ dimensions : [ 'DATE' ] ,
108
+ ...dateToJson ( 'startDate' , oneWeekAgo ) ,
109
+ ...dateToJson ( 'endDate' , today ) ,
94
110
// Sort by ascending date.
95
- sort : [ '+DATE' ]
111
+ orderBy : [ '+DATE' ]
96
112
} ) ;
97
113
98
114
if ( report . rows ) {
99
- var spreadsheet = SpreadsheetApp . create ( 'AdSense Report' ) ;
100
- var sheet = spreadsheet . getActiveSheet ( ) ;
115
+ const spreadsheet = SpreadsheetApp . create ( 'AdSense Report' ) ;
116
+ const sheet = spreadsheet . getActiveSheet ( ) ;
101
117
102
118
// Append the headers.
103
- var headers = report . headers . map ( function ( header ) {
104
- return header . name ;
105
- } ) ;
106
- sheet . appendRow ( headers ) ;
119
+ sheet . appendRow ( report . headers . map ( header => header . name ) ) ;
107
120
108
121
// Append the results.
109
- sheet . getRange ( 2 , 1 , report . rows . length , headers . length )
110
- . setValues ( report . rows ) ;
122
+ sheet . getRange ( 2 , 1 , report . rows . length , report . headers . length )
123
+ . setValues ( report . rows . map ( row => row . cells . map ( cell => cell . value ) ) ) ;
111
124
112
125
Logger . log ( 'Report spreadsheet created: %s' ,
113
- spreadsheet . getUrl ( ) ) ;
126
+ spreadsheet . getUrl ( ) ) ;
114
127
} else {
115
128
Logger . log ( 'No rows returned.' ) ;
116
129
}
@@ -121,7 +134,22 @@ function generateReport(adClientId) {
121
134
* @param {string } parameter The parameter to be escaped.
122
135
* @return {string } The escaped parameter.
123
136
*/
124
- function escapeFilterParameter ( parameter ) {
137
+ function escapeFilterParameter ( parameter ) {
125
138
return parameter . replace ( '\\' , '\\\\' ) . replace ( ',' , '\\,' ) ;
126
139
}
140
+
141
+ /**
142
+ * Returns the JSON representation of a Date object (as a google.type.Date).
143
+ *
144
+ * @param {string } paramName the name of the date parameter
145
+ * @param {Date } value the date
146
+ */
147
+ function dateToJson ( paramName , value ) {
148
+ return {
149
+ [ paramName + '.year' ] : value . getFullYear ( ) ,
150
+ [ paramName + '.month' ] : value . getMonth ( ) + 1 ,
151
+ [ paramName + '.day' ] : value . getDate ( )
152
+ } ;
153
+ }
154
+
127
155
// [END apps_script_adsense_generate_report]
0 commit comments