質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

918閲覧

JSONデータから特定の情報をうまく抽出できない。

Shinog

総合スコア99

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2017/10/03 12:31

編集2017/10/03 12:32

お世話になっております。
下記URLをもとに、Google AnalyticsのMAU情報を取得したいと考えており、JSONデータを取得するところまでは行けたのですが、そこからMAUのみを抽出する部分でつまづいております。

https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-js?hl=ja

html

1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <title>Hello Analytics - A quickstart guide for JavaScript</title> 6</head> 7<body> 8 9<button id="auth-button" hidden>Authorize</button> 10 11<h1>Hello Analytics</h1> 12 13<textarea cols="80" rows="20" id="query-output"></textarea> 14 15<script> 16 17 // Replace with your client ID from the developer console. 18 var CLIENT_ID = '<YOUR_CLIENT_ID>'; 19 20 // Set authorized scope. 21 var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']; 22 23 24 function authorize(event) { 25 // Handles the authorization flow. 26 // `immediate` should be false when invoked from the button click. 27 var useImmdiate = event ? false : true; 28 var authData = { 29 client_id: CLIENT_ID, 30 scope: SCOPES, 31 immediate: useImmdiate 32 }; 33 34 gapi.auth.authorize(authData, function(response) { 35 var authButton = document.getElementById('auth-button'); 36 if (response.error) { 37 authButton.hidden = false; 38 } 39 else { 40 authButton.hidden = true; 41 queryAccounts(); 42 } 43 }); 44 } 45 46 47function queryAccounts() { 48 // Load the Google Analytics client library. 49 gapi.client.load('analytics', 'v3').then(function() { 50 51 // Get a list of all Google Analytics accounts for this user 52 gapi.client.analytics.management.accounts.list().then(handleAccounts); 53 }); 54} 55 56 57function handleAccounts(response) { 58 // Handles the response from the accounts list method. 59 if (response.result.items && response.result.items.length) { 60 // Get the first Google Analytics account. 61 var firstAccountId = response.result.items[0].id; 62 63 // Query for properties. 64 queryProperties(firstAccountId); 65 } else { 66 console.log('No accounts found for this user.'); 67 } 68} 69 70 71function queryProperties(accountId) { 72 // Get a list of all the properties for the account. 73 gapi.client.analytics.management.webproperties.list( 74 {'accountId': accountId}) 75 .then(handleProperties) 76 .then(null, function(err) { 77 // Log any errors. 78 console.log(err); 79 }); 80} 81 82 83function handleProperties(response) { 84 // Handles the response from the webproperties list method. 85 if (response.result.items && response.result.items.length) { 86 87 // Get the first Google Analytics account 88 var firstAccountId = response.result.items[0].accountId; 89 90 // Get the first property ID 91 var firstPropertyId = response.result.items[0].id; 92 93 // Query for Views (Profiles). 94 queryProfiles(firstAccountId, firstPropertyId); 95 } else { 96 console.log('No properties found for this user.'); 97 } 98} 99 100 101function queryProfiles(accountId, propertyId) { 102 // Get a list of all Views (Profiles) for the first property 103 // of the first Account. 104 gapi.client.analytics.management.profiles.list({ 105 'accountId': accountId, 106 'webPropertyId': propertyId 107 }) 108 .then(handleProfiles) 109 .then(null, function(err) { 110 // Log any errors. 111 console.log(err); 112 }); 113} 114 115 116function handleProfiles(response) { 117 // Handles the response from the profiles list method. 118 if (response.result.items && response.result.items.length) { 119 // Get the first View (Profile) ID. 120 var firstProfileId = response.result.items[0].id; 121 122 // Query the Core Reporting API. 123 queryCoreReportingApi(firstProfileId); 124 } else { 125 console.log('No views (profiles) found for this user.'); 126 } 127} 128 129 130function queryCoreReportingApi(profileId) { 131 // Query the Core Reporting API for the number sessions for 132 // the past seven days. 133 gapi.client.analytics.data.ga.get({ 134 'ids': 'ga:' + profileId, 135 'start-date': '30daysAgo', 136 'end-date': 'today', 137 'metrics': 'ga:users' 138 }) 139 .then(function(response) { 140 var formattedJson = JSON.stringify(response.result, null, 2); 141 document.getElementById('query-output').value = formattedJson; 142 }) 143 .then(null, function(err) { 144 // Log any errors. 145 console.log(err); 146 }); 147} 148 149 // Add an event listener to the 'auth-button'. 150 document.getElementById('auth-button').addEventListener('click', authorize); 151</script> 152 153<script src="https://apis.google.com/js/client.js?onload=authorize"></script> 154 155</body> 156</html> 157

上記コードの下記部分の
document.getElementById('query-output').value = formattedJson

document.getElementById('query-output').value = formattedJson. totalsForAllResults.ga:users

document.getElementById('query-output').value = formattedJson["totalsForAllResults"]["ga:users"]

などに変更すると、データが一切表示されなくなてしまいます。

html

1function queryCoreReportingApi(profileId) { 2 // Query the Core Reporting API for the number sessions for 3 // the past seven days. 4 gapi.client.analytics.data.ga.get({ 5 'ids': 'ga:' + profileId, 6 'start-date': '30daysAgo', 7 'end-date': 'today', 8 'metrics': 'ga:users' 9 }) 10 .then(function(response) { 11 var formattedJson = JSON.stringify(response.result, null, 2); 12 document.getElementById('query-output').value = formattedJson; 13 }) 14 .then(null, function(err) { 15 // Log any errors. 16 console.log(err); 17 }); 18}

json

1{ 2 "kind": "analytics#gaData", 3 "id": "https://www.googleapis.com/analytics/v3/data/ga?ids=<YOUR_CLIENT_ID>&metrics=ga:users&start-date=30daysAgo&end-date=today", 4 "query": { 5 "start-date": "30daysAgo", 6 "end-date": "today", 7 "ids": "<YOUR_CLIENT_ID>", 8 "metrics": [ 9 "ga:users" 10 ], 11 "start-index": 1, 12 "max-results": 1000 13 }, 14 "itemsPerPage": 1000, 15 "totalResults": 1, 16 "selfLink": "https://www.googleapis.com/analytics/v3/data/ga?ids=<YOUR_CLIENT_ID>&metrics=ga:users&start-date=30daysAgo&end-date=today", 17 "profileInfo": { 18 "profileId": "<省略>", 19 "accountId": "<省略>", 20 "webPropertyId": "<省略>", 21 "internalWebPropertyId": "<省略>", 22 "profileName": "すべてのウェブサイトのデータ", 23 "tableId": "<省略>" 24 }, 25 "containsSampledData": false, 26 "columnHeaders": [ 27 { 28 "name": "ga:users", 29 "columnType": "METRIC", 30 "dataType": "INTEGER" 31 } 32 ], 33 "totalsForAllResults": { 34 "ga:users": "93005" 35 }, 36 "rows": [ 37 [ 38 "93005" 39 ] 40 ] 41}

この場合、どのような処理を行えば良いでしょうか。
何卒よろしくお願いします!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

下記ではどうでしょうか。

formattedJson.totalsForAllResults['ga:users']


追記:

JSON.stringifyをかけているからのようです。

javascript

1var formattedJson = response.result; 2document.getElementById('query-output').value = formattedJson.totalsForAllResults['ga:users'];

投稿2017/10/03 12:38

編集2017/10/03 13:25
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Shinog

2017/10/03 13:22 編集

表示されなかったです、、 また、Chromeの検証を行ったところ、何かエラーが出るということもありませんでした、、
退会済みユーザー

退会済みユーザー

2017/10/03 13:29

JSONデータの確認しかできていませんでした。 直前で文字列として変換をかけられているため、JSONデータとして扱えていないようです。 formattedJsonは文字列表現として利用される想定の変数であれば、別の変数で取り扱ってください。
Shinog

2017/10/10 11:09

返信が遅くなり、大変申し訳ありません!! JSON.stringifyを取り除くことで、無事解決できました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問