forked from googleapis/google-api-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateReportWithPaging.html
More file actions
123 lines (107 loc) · 4.18 KB
/
GenerateReportWithPaging.html
File metadata and controls
123 lines (107 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<!--
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
This example retrieves a report for the specified ad client.
Please only use pagination if your application requires it due to memory or
storage constraints.
If you need to retrieve more than 5000 rows, please check GenerateReport.html,
as due to current limitations you will not be able to use paging for large reports.
To get ad clients, run GetAllAdClients.html.
Tags: reports.generate
Author silvano.luciani@gmail.com (Silvano Luciani)
-->
<head>
<title>Generate report with paging</title>
<script type="text/javascript" src="auth.js"></script>
<script type="text/javascript" src="util.js"></script>
</head>
<body>
<div id="content"></div>
<!-- Add a button for the user to click to initiate auth sequence -->
<button id="authorize-button" style="visibility: hidden">Authorize</button>
<script type="text/javascript">
AD_CLIENT_ID = 'INSERT_THE_AD_CLIENT_ID_HERE';
MAX_PAGE_SIZE = 5;
var startIndex = 0;
// Now.
var endDate = new Date();
// Six months ago.
var startDate = new Date(endDate.getTime() - 1000 * 60 * 60 * 24 * 180);
// Gets and renders the data.
function makeApiCall() {
gapi.client.load('adsense', 'v1.1', function() {
var reqParams = {
'startDate': formatDate(startDate),
'endDate': formatDate(endDate),
'filter': ['AD_CLIENT_ID==' + AD_CLIENT_ID],
'metric': ['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE',
'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK',
'AD_REQUESTS_RPM'],
'dimension': ['MONTH'],
'sort': ['MONTH'],
'maxResults': MAX_PAGE_SIZE
};
var reportTable = document.createElement('table');
executeRequest(reqParams, startIndex, reportTable);
document.getElementById('content').appendChild(reportTable);
});
}
// Executes the request recursively.
function executeRequest(reqParams, startIndex, reportTable) {
reqParams.startIndex = startIndex;
request = gapi.client.adsense.reports.generate(reqParams);
request.execute(function(resp) {
totalMatchedRows = resp.totalMatchedRows;
if(startIndex == 0) {
addTableHeaders(reportTable, resp.headers);
}
rowTotal = resp.rows.length;
startIndex += MAX_PAGE_SIZE;
for (var rowIndex = 0; rowIndex < rowTotal; rowIndex++) {
addTableRow(reportTable, resp.rows[rowIndex]);
}
if(startIndex < resp.totalMatchedRows) {
executeRequest(reqParams, startIndex, reportTable);
}
});
}
// Adds headers to the report table.
function addTableHeaders(table, headers) {
var headerRow = document.createElement('tr');
var headersTotal = headers.length;
for (var key = 0; key < headersTotal; key++) {
var header = headers[key];
var headerElem = document.createElement('th');
headerElem.appendChild(document.createTextNode(header.name));
headerRow.appendChild(headerElem);
}
table.appendChild(headerRow);
}
// Adds a row to the report table.
function addTableRow(table, item) {
var row = document.createElement('tr');
var colTotal = item.length;
for (var colIndex = 0; colIndex < colTotal; colIndex++) {
var rowElem = document.createElement('td');
rowElem.appendChild(document.createTextNode(
item[colIndex]));
row.appendChild(rowElem);
}
table.appendChild(row);
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</body>
</html>