{"id":32488,"date":"2016-02-09T20:21:13","date_gmt":"2016-02-09T14:51:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32488"},"modified":"2016-06-30T10:13:34","modified_gmt":"2016-06-30T04:43:34","slug":"integrate-google-analytics-api-to-fetch-page-views-count","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrate-google-analytics-api-to-fetch-page-views-count\/","title":{"rendered":"Integrate Google Analytics Api to fetch Page views count"},"content":{"rendered":"<p>My use case was to show count of Page Views and Unique Page Views using <a href=\"http:\/\/www.tothenew.com\/blog\/click-event-tracking-using-google-tag-manager-v2\/\">Google analytics<\/a>. Though there is already enough documentation provided with detailed explanation, it takes time to understand where to get required parameters to call those APIs, due to nested structure of Analytics account.<\/p>\n<p>We can refer <a href=\"https:\/\/support.google.com\/analytics\/answer\/1009618?hl=en&amp;ref_topic=1102143\">here<\/a> to get detailed idea on Hierarchy of accounts, users, properties, and views for Google Analytics<\/p>\n<p>To fetch Page views and Unique Page views count it&#8217;s a Profile Id that we need to know, not a Analytics account Id.<\/p>\n<p>Below are required api url with scenarios and purpose it is for:<\/p>\n<p><em>Scenario &#8211; A single Google Account can have more than Analytics account<\/em><\/p>\n<p><strong>Api<\/strong> : https:\/\/www.googleapis.com\/analytics\/v3\/management\/accounts<br \/>\n<strong>Purpose<\/strong> : To fetch all Analytics accounts associated with a particular Google Account<br \/>\n<strong>Reference<\/strong> : https:\/\/developers.google.com\/analytics\/devguides\/config\/mgmt\/v3\/mgmtReference<br \/>\n\/management\/accounts\/list<\/p>\n<p><em>Scenario &#8211; A single Analytics account can have more than one Webproperty<\/em><\/p>\n<p><strong>Api<\/strong> : https:\/\/www.googleapis.com\/analytics\/v3\/management\/accounts\/accountId\/webproperties<br \/>\n<strong>Purpose<\/strong> : To fetch all web properties associated with a specific Analytics account.<br \/>\n<strong>Reference<\/strong> : https:\/\/developers.google.com\/analytics\/devguides\/config\/mgmt\/v3\/mgmtReference<br \/>\n\/management\/webproperties\/list<\/p>\n<p><em>Scenario &#8211; A single Webproperty can have more than one Profiles<\/em><\/p>\n<p><strong>Api<\/strong> : https:\/\/www.googleapis.com\/analytics\/v3\/management\/accounts\/accountId\/webproperties<br \/>\n\/webPropertyId\/profiles<br \/>\n<strong>Purpose<\/strong> : To fetch all Profiles associated under a webproperty<br \/>\n<strong>Reference<\/strong> : https:\/\/developers.google.com\/analytics\/devguides\/config\/mgmt\/v3\/mgmtReference<br \/>\n\/management\/profiles\/list<\/p>\n<p>Once we have desired profile id, we can use below api to fetch Page views and visitors count.<\/p>\n<p><strong>Api<\/strong> : https:\/\/www.googleapis.com\/analytics\/v3\/data\/ga?ids=ga:{profileId}&amp;start-date={yyyy-mm- dd}&amp;end-date={yyyy-mm-dd}&amp;metrics=ga:pageviews,ga:uniquePageviews &#8211; <a href=\"https:\/\/ga-dev-tools.appspot.com\/query-explorer\/\">Reference<\/a><\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My use case was to show count of Page Views and Unique Page Views using Google analytics. Though there is already enough documentation provided with detailed explanation, it takes time to understand where to get required parameters to call those APIs, due to nested structure of Analytics account. We can refer here to get detailed [&hellip;]<\/p>\n","protected":false},"author":230,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":27},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32488"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/230"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32488"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32488\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}