|
1 |
| -<script> |
2 |
| -(function(w,d,s,g,js,fs){ |
3 |
| - g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}}; |
4 |
| - js=d.createElement(s);fs=d.getElementsByTagName(s)[0]; |
5 |
| - js.src='https://apis.google.com/js/platform.js'; |
6 |
| - fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');}; |
7 |
| -}(window,document,'script')); |
8 |
| -</script> |
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Hello Analytics Reporting API V4</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | +<button id="auth-button" hidden>Authorize</button> |
| 10 | + |
| 11 | +<h1>Hello Analytics Reporting API V4</h1> |
| 12 | + |
| 13 | +<textarea cols="80" rows="20" id="query-output"></textarea> |
9 | 14 |
|
10 |
| -<div id="embed-api-auth-container"></div> |
11 |
| -<div id="chart-container"></div> |
12 |
| -<div id="view-selector-container"></div> |
13 | 15 | <script>
|
14 | 16 |
|
15 |
| -gapi.analytics.ready(function() { |
16 |
| - |
17 |
| - /** |
18 |
| - * Authorize the user immediately if the user has already granted access. |
19 |
| - * If no access has been created, render an authorize button inside the |
20 |
| - * element with the ID "embed-api-auth-container". |
21 |
| - */ |
22 |
| - gapi.analytics.auth.authorize({ |
23 |
| - container: 'embed-api-auth-container', |
24 |
| - clientid: '1020428475379-i265lih95a3ips9u6j5c0so8hmpg21kk.apps.googleusercontent.com' |
25 |
| - }); |
26 |
| - |
27 |
| - |
28 |
| - /** |
29 |
| - * Create a new ViewSelector instance to be rendered inside of an |
30 |
| - * element with the id "view-selector-container". |
31 |
| - */ |
32 |
| - var viewSelector = new gapi.analytics.ViewSelector({ |
33 |
| - container: 'view-selector-container' |
34 |
| - }); |
35 |
| - |
36 |
| - // Render the view selector to the page. |
37 |
| - viewSelector.execute(); |
38 |
| - |
39 |
| - |
40 |
| - /** |
41 |
| - * Create a new DataChart instance with the given query parameters |
42 |
| - * and Google chart options. It will be rendered inside an element |
43 |
| - * with the id "chart-container". |
44 |
| - */ |
45 |
| - var dataChart = new gapi.analytics.googleCharts.DataChart({ |
46 |
| - query: { |
47 |
| - metrics: 'ga:sessions', |
48 |
| - dimensions: 'ga:date', |
49 |
| - 'start-date': '30daysAgo', |
50 |
| - 'end-date': 'yesterday' |
51 |
| - }, |
52 |
| - chart: { |
53 |
| - container: 'chart-container', |
54 |
| - type: 'LINE', |
55 |
| - options: { |
56 |
| - width: '100%' |
| 17 | + // Replace with your client ID from the developer console. |
| 18 | + var CLIENT_ID = '1020428475379-i265lih95a3ips9u6j5c0so8hmpg21kk.apps.googleusercontent.com'; |
| 19 | + |
| 20 | + // Replace with your view ID. |
| 21 | + var VIEW_ID = '115222200'; |
| 22 | + |
| 23 | + // Set the discovery URL. |
| 24 | + var DISCOVERY = 'https://analyticsreporting.googleapis.com/$discovery/rest'; |
| 25 | + |
| 26 | + // Set authorized scope. |
| 27 | + var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']; |
| 28 | + |
| 29 | + |
| 30 | + function authorize(event) { |
| 31 | + // Handles the authorization flow. |
| 32 | + // `immediate` should be false when invoked from the button click. |
| 33 | + var useImmdiate = event ? false : true; |
| 34 | + var authData = { |
| 35 | + client_id: CLIENT_ID, |
| 36 | + scope: SCOPES, |
| 37 | + immediate: useImmdiate |
| 38 | + }; |
| 39 | + |
| 40 | + gapi.auth.authorize(authData, function(response) { |
| 41 | + var authButton = document.getElementById('auth-button'); |
| 42 | + if (response.error) { |
| 43 | + authButton.hidden = false; |
| 44 | + } |
| 45 | + else { |
| 46 | + authButton.hidden = true; |
| 47 | + queryReports(); |
57 | 48 | }
|
58 |
| - } |
59 |
| - }); |
| 49 | + }); |
| 50 | + } |
60 | 51 |
|
| 52 | + function queryReports() { |
| 53 | + // Load the API from the client discovery URL. |
| 54 | + gapi.client.load(DISCOVERY |
| 55 | + ).then(function() { |
61 | 56 |
|
62 |
| - /** |
63 |
| - * Render the dataChart on the page whenever a new view is selected. |
64 |
| - */ |
65 |
| - viewSelector.on('change', function(ids) { |
66 |
| - dataChart.set({query: {ids: ids}}).execute(); |
67 |
| - }); |
| 57 | + // Call the Analytics Reporting API V4 batchGet method. |
| 58 | + gapi.client.analyticsreporting.reports.batchGet( { |
| 59 | + "reportRequests":[ |
| 60 | + { |
| 61 | + "viewId":VIEW_ID, |
| 62 | + "dateRanges":[ |
| 63 | + { |
| 64 | + "startDate":"7daysAgo", |
| 65 | + "endDate":"today" |
| 66 | + }], |
| 67 | + "metrics":[ |
| 68 | + { |
| 69 | + "expression":"ga:sessions" |
| 70 | + }] |
| 71 | + }] |
| 72 | + } ).then(function(response) { |
| 73 | + var formattedJson = JSON.stringify(response.result, null, 2); |
| 74 | + document.getElementById('query-output').value = formattedJson; |
| 75 | + }) |
| 76 | + .then(null, function(err) { |
| 77 | + // Log any errors. |
| 78 | + console.log(err); |
| 79 | + }); |
| 80 | + }); |
| 81 | + } |
68 | 82 |
|
69 |
| -}); |
| 83 | + // Add an event listener to the 'auth-button'. |
| 84 | + document.getElementById('auth-button').addEventListener('click', authorize); |
70 | 85 | </script>
|
| 86 | + |
| 87 | +<script src="https://apis.google.com/js/client.js?onload=authorize"></script> |
| 88 | + |
| 89 | +</body> |
| 90 | +</html> |
| 91 | + |
0 commit comments