{"id":4422,"date":"2016-11-11T10:00:31","date_gmt":"2016-11-11T01:00:31","guid":{"rendered":"http:\/\/sitest.jp\/blog\/?p=4422"},"modified":"2025-09-09T14:05:17","modified_gmt":"2025-09-09T05:05:17","slug":"node-js%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6google-analytics-api%e3%81%8b%e3%82%89%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e3%81%a6%e3%81%bf%e3%82%88%e3%81%86","status":"publish","type":"post","link":"https:\/\/sitest.jp\/blog\/?p=4422","title":{"rendered":"Node.js\u3092\u4f7f\u3063\u3066Google Analytics API\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u3088\u3046"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30d7\u30ed\u30c0\u30af\u30c8\u958b\u767a\u30c1\u30fc\u30e0\u306e\u7267\u3067\u3059\u3002<br \/>\n\u6700\u8fd1\u3001Google Analytics\u3092\u89e6\u308b\u3053\u3068\u304c\u5897\u3048\u3066\u304d\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001<br \/>\n\u305b\u3063\u304b\u304f\u306a\u306e\u3067Google Analytics API\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u4eca\u56de\u306f\u3001Node.js\u3092\u4f7f\u3063\u3066Google Analytics\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<div class=\"cta_wrap\">\n  <a href=\"https:\/\/sitest.jp\/ai-analyze\/\" class=\"blog_cta_bannar\" target=\"_brank\" rel=\"noopener noreferrer\"><br \/>\n    <img src=\"https:\/\/sitest.jp\/images\/blog\/ai-analyze_blog2.png\" alt=\"AI\u8a3a\u65ad\uff08\u03b2\u7248\uff09\"><br \/>\n  <\/a><\/p>\n<style>\n    .blog_cta_bannar:hover {\n      opacity: 0.8;\n    }\n  <\/style>\n<\/div>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Google Analytics API<\/h3>\n<\/div>\n<p>Google\u306f\u63d0\u4f9b\u3057\u3066\u3044\u308b\u30b5\u30fc\u30d3\u30b9\u306e\u307b\u3068\u3093\u3069\u3092API\u3092\u901a\u3058\u3066\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\nGoogle Analytics\u3067\u306f\u4ee5\u4e0b\u306eAPI\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Core Reporting API<\/li>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Embed API<\/li>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Multi-Channel Funnels Reporting API<\/li>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Real Time Reporting API<\/li>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Metadata API<\/li>\n<li style=\"list-style-type: disc; margin-left: 20px;\">Management API<\/li>\n<\/ul>\n<p>\u7528\u9014\u306b\u5fdc\u3058\u3066\u69d8\u3005\u306aAPI\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u304c\u3001\u4eca\u56de\u306f<a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/core\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Core Reporting API<\/a>\u3092\u4f7f\u7528\u3057\u3066\u9032\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">OAuth\u8a8d\u8a3c\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3088\u3046<\/h3>\n<\/div>\n<p>Google Analytics API\u3092\u5229\u7528\u3059\u308b\u306b\u306fOAuth\u8a8d\u8a3c\u3092\u884c\u3044\u3001\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u767a\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\nGoogle API Manager\u304b\u3089\u300cOAuth \u30af\u30e9\u30a4\u30a2\u30f3\u30c8 ID\u300d\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u7a2e\u985e\u306f\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u9078\u629e\u3057\u3001\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u5148\u306f\u300chttp:\/\/localhost\u300d\u3068\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8ID\u3068\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u304c\u767a\u884c\u3055\u308c\u307e\u3059\u306e\u3067\u3001\u4fdd\u5b58\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Node.js\u3067\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3088\u3046<\/h3>\n<\/div>\n<h4 style=\"font-weight: bold;\">npm\u3067\u30e2\u30b8\u30e5\u30fc\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n<p>Google Analytics API\u306fREST-API\u3068\u3057\u3066\u3082\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u3001HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3063\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u304c\u3001<br \/>\n\u4eca\u56de\u306f<a href=\"https:\/\/github.com\/google\/google-api-nodejs-client\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">google-api-node-client<\/a>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/google_google-api-nodejs-client__Google_s_officially_supported_Node_js_client_library_for_accessing_Google_APIs__Support_for_authorization_and_authentication_with_OAuth_2_0__API_Keys_and_JWT__Service_Tokens__is_included__API_Reference_Docs_.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-4507\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/google_google-api-nodejs-client__Google_s_officially_supported_Node_js_client_library_for_accessing_Google_APIs__Support_for_authorization_and_authentication_with_OAuth_2_0__API_Keys_and_JWT__Service_Tokens__is_included__API_Reference_Docs_-1024x465.png\" alt=\"google_google-api-nodejs-client__Google_s_officially_supported_Node_js_client_library_for_accessing_Google_APIs__Support_for_authorization_and_authentication_with_OAuth_2_0__API_Keys_and_JWT__Service_Tokens__is_included__API_Reference_Docs_\" width=\"604\" height=\"274\" \/><\/a><br \/>\n\u3053\u3061\u3089\u306fGoogle\u304cOSS\u3067\u63d0\u4f9b\u3057\u3066\u3044\u308bNode.js\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4ee5\u4e0b\u3067npm\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre><code>$ cd \/path\/to\/project\r\n$ npm init -y\r\n$ npm install --save googleapis\r\n<\/code><\/pre>\n<p>node_modules\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4ee5\u4e0b\u306bgoogleapis\u304c\u4fdd\u5b58\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4 style=\"font-weight: bold;\">app.js\u306e\u4f5c\u6210<\/h4>\n<p>\u305d\u308c\u3067\u306f\u65e9\u901f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u306f\u65b0\u898f\u30e6\u30fc\u30b6\u3068\u30ea\u30d4\u30fc\u30c8\u30e6\u30fc\u30b6\u6bce\u306b<br \/>\n\u30bb\u30c3\u30b7\u30e7\u30f3\u6570\u3001\u65b0\u898f\u30bb\u30c3\u30b7\u30e7\u30f3\u7387\u3001\u76f4\u5e30\u7387\u3092\u904e\u53bb1\u30f6\u6708\u5206\u53d6\u5f97\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30b3\u30fc\u30c9\u4f8b\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>var readline = require(\"readline\");\r\nvar googleapis = require(\"googleapis\");\r\n\r\nvar CLIENT_ID = \"CLIENT ID\",\r\nCLIENT_SECRET = \"CLIENT SECRET\",\r\nREDIRECT_URL = \"http:\/\/localhost\",\r\nSCOPE = \"https:\/\/www.googleapis.com\/auth\/analytics.readonly\",\r\nVIEW_ID = \"ANALYTICS VIEW ID\";\r\n\r\nvar rl = readline.createInterface({input: process.stdin, output: process.stdout});\r\nvar auth = new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);\r\nvar url = auth.generateAuthUrl({scope: SCOPE});\r\n\r\nvar getAccessToken = function(code){\r\nauth.getToken(code, function(err, tokens){\r\nauth.setCredentials(tokens);\r\ngetGaData()\r\n});\r\n}\r\n\r\nvar getGaData = function(){\r\nvar analytics = googleapis.analytics({auth: auth, version: \"v3\"});\r\nanalytics.data.ga.get({\r\nids: \"ga:\" + VIEW_ID,\r\nstart-date: \"30daysAgo\",\r\nend-date: \"yesterday\",\r\nmetrics: \"ga:users,ga:percentNewSessions,ga:bounceRate\",\r\ndimensions: \"ga:userType\"\r\n}, function(err, rows){\r\nconsole.log(rows);\r\n});\r\n}\r\n\r\nconsole.log(\"Visit the url: \" + url);\r\nrl.question(\"Enter then code hear: \", getAccessToken);\r\n<\/code><\/pre>\n<p>\u307e\u305a\u3001\u5148\u307b\u3069\u53d6\u5f97\u3057\u305fOAuth\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8ID\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u3092\u7f6e\u304d\u63db\u3048\u307e\u3059\u3002<br \/>\n\u307e\u305fVIEW_ID\u306fGoogle Analytics\u306e\u30d3\u30e5\u30fc\u306e\u8a2d\u5b9a\u753b\u9762\u304b\u3089\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/1462271471160f5704ccca1346e4f277.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-4516\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/1462271471160f5704ccca1346e4f277.png\" alt=\"\u30a2\u30ca\u30ea\u30c6\u30a3\u30af\u30b9\" width=\"999\" height=\"632\" \/><\/a><\/p>\n<p>\u3053\u308c\u3060\u3051\u3067\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u5b8c\u6210\u3067\u3059\u3002<br \/>\n\u3067\u306f\u5b9f\u969b\u306b\u52d5\u304b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff01<\/p>\n<div class=\"cta_wrap\">\n  <a href=\"https:\/\/sitest.jp\/ai-analyze\/\" class=\"blog_cta_bannar\" target=\"_brank\" rel=\"noopener noreferrer\"><br \/>\n    <img src=\"https:\/\/sitest.jp\/images\/blog\/ai-analyze_blog2.png\" alt=\"AI\u8a3a\u65ad\uff08\u03b2\u7248\uff09\"><br \/>\n  <\/a><\/p>\n<style>\n    .blog_cta_bannar:hover {\n      opacity: 0.8;\n    }\n  <\/style>\n<\/div>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u3088\u3046<\/h3>\n<\/div>\n<p>node\u30b3\u30de\u30f3\u30c9\u3067\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre><code>$ node app.js\r\nVisit the url: Authentication URL...\r\nEnter then code hear:\r\n<\/code><\/pre>\n<p>\u767a\u884c\u3055\u308c\u305fURL\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u8a8d\u8a3c\u753b\u9762\u304c\u51fa\u3066\u304d\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/fa6c054ac417ee8ab0d97c6fe2ac0cf2.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-4518\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2016\/09\/fa6c054ac417ee8ab0d97c6fe2ac0cf2-1024x586.png\" alt=\"\u8a31\u53ef\u306e\u30ea\u30af\u30a8\u30b9\u30c8\" width=\"604\" height=\"345\" \/><\/a><br \/>\n\u8a31\u53ef\u3092\u3059\u308b\u3068\u8a2d\u5b9a\u3057\u305f\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u5148\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u307e\u3059\u3002<br \/>\nURL\u306b\u306fcode\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u4ed8\u4e0e\u3055\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u300ccode=\u300d\u306e\u5f8c\u3092\u30b3\u30d4\u30fc\u3057\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u8cbc\u308a\u4ed8\u3051\u3066\u4e0b\u3055\u3044\u3002<br \/>\n\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u306b\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\u307e\u3068\u3081<\/h3>\n<\/div>\n<p>\u4eca\u56de\u306fOAuth\u306e\u8a8d\u8a3c\u304b\u3089API\u3092\u5229\u7528\u3057\u3066Google Analytics\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u307e\u3067\u3092\u884c\u3044\u307e\u3057\u305f\u3002<br \/>\n\u57fa\u672c\u7684\u306bAnalytics\u306e\u30ec\u30dd\u30fc\u30c8\u3067\u51fa\u308b\u30c7\u30fc\u30bf\u306fAPI\u3067\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u306e\u3067\u3001<br \/>\n\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u305f\u72ec\u81ea\u30ec\u30dd\u30fc\u30c8\u3092\u4f5c\u308a\u305f\u3044\u5834\u5408\u306a\u3069\u306b\u3068\u3066\u3082\u6709\u7528\u3067\u3059\u3002<\/p>\n<p>Google Analytics\u306eAPI\u306f\u4eca\u56de\u3054\u7d39\u4ecb\u3057\u305fCoreReportingAPI\u4ee5\u5916\u306b\u3082\u591a\u304f\u306eAPI\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001<br \/>\n\u4eca\u5f8c\u3082\u3054\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<div class=\"cta_wrap\">\n  <a href=\"https:\/\/sitest.jp\/ai-analyze\/\" class=\"blog_cta_bannar\" target=\"_brank\" rel=\"noopener noreferrer\"><br \/>\n    <img src=\"https:\/\/sitest.jp\/images\/blog\/ai-analyze_blog2.png\" alt=\"AI\u8a3a\u65ad\uff08\u03b2\u7248\uff09\"><br \/>\n  <\/a><\/p>\n<style>\n    .blog_cta_bannar:hover {\n      opacity: 0.8;\n    }\n  <\/style>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30d7\u30ed\u30c0\u30af\u30c8\u958b\u767a\u30c1\u30fc\u30e0\u306e\u7267\u3067\u3059\u3002 \u6700\u8fd1\u3001Google Analytics\u3092\u89e6\u308b\u3053\u3068\u304c\u5897\u3048\u3066\u304d\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001 \u305b\u3063\u304b\u304f\u306a\u306e\u3067Google Analytics API\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u4eca\u56de\u306f\u3001Node.js\u3092\u4f7f\u3063\u3066Google Analytics\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,27],"tags":[],"acf":{"keywords":"Node.js,Google Analytics,API","description":"Node.js\u3092\u4f7f\u7528\u3057\u3066Google Analytics\u304b\u3089API\u7d4c\u7531\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002","ogp_url":"","banner":""},"_links":{"self":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/4422"}],"collection":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/comments?post=4422"}],"version-history":[{"count":17,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/4422\/revisions"}],"predecessor-version":[{"id":32396,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/4422\/revisions\/32396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media?parent=4422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/categories?post=4422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/tags?post=4422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}