// Copyright 2012 Google Inc. All Rights Reserved. /* 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. */ /** * @fileoverview Reference example for the Core Reporting API. This example * demonstrates how to access the important information from version 3 of * the Google Analytics Core Reporting API. * @author api.nickm@gmail.com (Nick Mihailovski) */ // Simple place to store all the results before printing to the user. var output = []; // Initialize the UI Dates. document.getElementById('start-date').value = lastNDays(14); document.getElementById('end-date').value = lastNDays(0); /** * Executes a Core Reporting API query to retrieve the top 25 organic search * terms. Once complete, handleCoreReportingResults is executed. Note: A user * must have gone through the Google APIs authorization routine and the Google * Anaytics client library must be loaded before this function is called. */ function makeApiCall() { gapi.client.analytics.data.ga.get({ 'ids': document.getElementById('table-id').value, 'start-date': document.getElementById('start-date').value, 'end-date': document.getElementById('end-date').value, 'metrics': 'ga:visits', 'dimensions': 'ga:source,ga:keyword', 'sort': '-ga:visits,ga:source', 'filters': 'ga:medium==organic', 'max-results': 25 }).execute(handleCoreReportingResults); } /** * Handles the response from the CVore Reporting API. If sucessful, the * results object from the API is passed to various printing functions. * If there was an error, a message with the error is printed to the user. * @param {Object} results The object returned from the API. */ function handleCoreReportingResults(results) { if (!results.code) { outputToPage('Query Success'); printReportInfo(results); printPaginationInfo(results); printProfileInfo(results); printQuery(results); printColumnHeaders(results); printTotalsForAllResults(results); printRows(results); outputToPage(output.join('')); } else { outputToPage('There was an error: ' + results.message); } } /** * Prints general information about this report. * @param {Object} results The object returned from the API. */ function printReportInfo(results) { output.push( '
',
'Contains Sampled Data = ', results.containsSampledData, '
',
'Kind = ', results.kind, '
',
'ID = ', results.id, '
',
'Self Link = ', results.selfLink, '
',
'
',
'Items Per Page = ', results.itemsPerPage, '
',
'Total Results = ', results.totalResults, '
',
'Previous Link = ',
results.previousLink ? results.previousLink : '', '
',
'Next Link = ',
results.nextLink ? results.nextLink : '', '
',
'
',
'Account Id = ', info.accountId, '
',
'Web Property Id = ', info.webPropertyId, '
',
'Profile Id = ', info.profileId, '
',
'Table Id = ', info.tableId, '
',
'Profile Name = ', info.profileName, '
',
'
');
for (var key in results.query) {
output.push(key, ' = ', results.query[key], '
');
}
output.push('
',
'Name = ', header.name, '
',
'Column Type = ', header.columnType, '
',
'Data Type = ', header.dataType, '
',
'
This query returned ', results.rows.length, ' rows. ', 'But the query matched ', results.totalResults, ' total results. ', 'Here are the metric totals for the matched total results.
'); var totals = results.totalsForAllResults; for (metricName in totals) { output.push( '',
'Metric Name = ', metricName, '
',
'Metric Total = ', totals[metricName], '
',
'
', header.name, ' | '); } table.push('|
---|---|
', row.join(' | '), ' |
No rows found.
'); } } /** * Utility method to update the output section of the HTML page. Used * to output messages to the user. This overwrites any existing content * in the output area. * @param {String} output The HTML string to output. */ function outputToPage(output) { document.getElementById('output').innerHTML = output; } /** * Utility method to update the output section of the HTML page. Used * to output messages to the user. This appends content to any existing * content in the output area. * @param {String} output The HTML string to output. */ function updatePage(output) { document.getElementById('output').innerHTML += '