{"id":2475,"date":"2023-01-19T12:03:28","date_gmt":"2023-01-19T11:03:28","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2475"},"modified":"2023-01-19T12:03:28","modified_gmt":"2023-01-19T11:03:28","slug":"angular-google-analytics","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/01\/19\/angular-google-analytics\/","title":{"rendered":"Angular &#038; Google Analytics"},"content":{"rendered":"\n<p>When someone asks about Tracking web pages for analysis, generally Google Analytics product by Google comes up first in your mind, Right?<\/p>\n\n\n\n<p id=\"8318\">Google Analytics collects data about your web presence from various sources.<\/p>\n\n\n\n<p id=\"abef\">There are three primary sources of this data:<\/p>\n\n\n\n<ol><li>User activities on your website or app<\/li><li>Any CRM and external data sources you provided to Google Analytics<\/li><li>The demographic information collected by Google about users visiting your website<\/li><\/ol>\n\n\n\n<p>Then, based on the configurations of your Google Analytics account (either through the Management API or User Interface), Google Analytics processes the data and convert those data into a format that is ready for analysis.<\/p>\n\n\n\n<p>\u00a0Anyone can use it to track and report the traffic on their website.<\/p>\n\n\n\n<p>In this article, we are going to see how with use of <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/data\/v1\">Google Analytics Data API<\/a>\u00a0 we can fetch data from\u00a0<a href=\"https:\/\/analytics.google.com\/\">Google Analytics<\/a>\u00a0in the simplest way.<\/p>\n\n\n\n<h4>Should I use Google Analytics with Angular?<\/h4>\n\n\n\n<p>Google Analytics is without doubt the most powerful and popular analytics tool that can be used for free. You can monitor all kinds of metrics like page views, sessions, bounces, visits and user engagement. It allows you to easily see how users are interacting with your website or application and make wiser marketing and business choices.<\/p>\n\n\n\n<p>It was originally created for static websites and wasn&#8217;t designed for single page applications created with Angular (or other web libraries and frameworks). This makes it a bit trickier to add Google Analytics to an Angular application but it is doable and really not that hard.<\/p>\n\n\n\n<p>If you want to discover how users are interacting with an Angular application then Google Analytics is definitely the cheapest, easiest and quickest way to get started.<\/p>\n\n\n\n<h4>Disadvantages of using Google Analytics with Angular<\/h4>\n\n\n\n<p>One of the biggest disadvantages of using Google Analytics is\u00a0<strong>privacy<\/strong>.<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\"><strong><em>PS:<\/em><\/strong><\/span> <em>remember also that most users that care about privacy have already installed firewalls and browser extensions to block Google Analytics.<\/em><\/p>\n\n\n\n<h4>How do you use Google Analytics with Angular?<\/h4>\n\n\n\n<p>When integrating Google Analytics into our Angular application we have two options.<\/p>\n\n\n\n<ul><li>Use a 3rd party library built by the open-source community.<\/li><li>Create our own Angular service that communicates with our Google Analytics account.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Obviously, the first choice is the easiest and quickest way to use Google Analytics with Angular. But it might not offer the customization and tracking that we need. In that case we&#8217;ll need to create our own service that communicates with the Google Analytics services.<\/p>\n\n\n\n<h4><a>Google Analytics Setup<\/a><\/h4>\n\n\n\n<p>Go to the <a>\u00a0<\/a><a href=\"https:\/\/analytics.google.com\/\">Google Analytics<\/a>\u00a0home page and create your account.<\/p>\n\n\n\n<p>If you already have an account then you can sign in instead.<\/p>\n\n\n\n<p>Next we&#8217;ll have to create a new property.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-9.png\" alt=\"\" class=\"wp-image-2486\" width=\"671\" height=\"401\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-9.png 860w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-9-300x179.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-9-768x459.png 768w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/figure><\/div>\n\n\n\n<p>Give your new property a name and properly configure the other options.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-10.png\" alt=\"\" class=\"wp-image-2487\" width=\"607\" height=\"350\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-10.png 837w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-10-300x173.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-10-768x443.png 768w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/figure><\/div>\n\n\n\n<p>Answer the business information questions and then click create.<\/p>\n\n\n\n<p>Then select your platform. In this case it will be\u00a0<code>Web<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-4.png\" alt=\"\" class=\"wp-image-2480\" width=\"547\" height=\"352\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-4.png 822w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-4-300x193.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-4-768x495.png 768w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/figure><\/div>\n\n\n\n<p>Now you&#8217;ll need to specify the URL of your website as well as the name of the new data stream (the data coming in from your Angular application to Google Analytics).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-5.png\" alt=\"\" class=\"wp-image-2481\" width=\"650\" height=\"358\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-5.png 868w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-300x166.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-5-768x424.png 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/figure><\/div>\n\n\n\n<p>This should take you to a page that displays the new web stream details. The\u00a0<code>MEASUREMENT ID<\/code>\u00a0is important and we&#8217;ll need it for the next step.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-6.png\" alt=\"\" class=\"wp-image-2482\" width=\"653\" height=\"383\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-6.png 876w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-6-300x176.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-6-768x452.png 768w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/figure><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>how to add Google Analytics to Angular application?<\/h4>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>Alternative 1<\/em><\/span><\/h4>\n\n\n\n<p>The first step is to install the\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/ngx-google-analytics\">ngx-google-analytics<\/a>\u00a0package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save ngx-google-analytics<\/code><\/pre>\n\n\n\n<p>Once installed, you&#8217;ll need to import it into your\u00a0<code>app.module.ts<\/code>\u00a0file like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { NgModule } from '@angular\/core';\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgxGoogleAnalyticsModule } from 'ngx-google-analytics';\r\n\r\nimport { AppRoutingModule } from '.\/app-routing.module';\r\nimport { AppComponent } from '.\/app.component';\r\n\r\n@NgModule({\r\n  declarations: &#91;\r\n    AppComponent,\r\n    NavBarComponent\r\n  ],\r\n  imports: &#91;\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    NgxGoogleAnalyticsModule.forRoot('MEASUREMENT-ID')\r\n  ],\r\n  providers: &#91;],\r\n  bootstrap: &#91;AppComponent]\r\n})\r\nexport class AppModule { }<\/code><\/pre>\n\n\n\n<p>Run your Angular application and you should see a new user show up in the Google Analytics real-time report.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-7.png\" alt=\"\" class=\"wp-image-2484\" width=\"568\" height=\"343\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-7.png 889w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-7-300x181.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-7-768x464.png 768w\" sizes=\"(max-width: 568px) 100vw, 568px\" \/><\/figure><\/div>\n\n\n\n<p><strong>There&#8217;s a problem though.&nbsp;<\/strong>?<\/p>\n\n\n\n<p>With the current configuration the Google Analytics package for Angular is&nbsp;<strong>not<\/strong>&nbsp;sending the various page views to the analytics service. This means that as a user navigates your Angular application you won&#8217;t be able to see what pages they&#8217;re navigating to.<\/p>\n\n\n\n<p>To fix this all we have to do is import the\u00a0<code>NgxGoogleAnalyticsRouterModule<\/code>\u00a0module. Like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { HttpClientModule } from '@angular\/common\/http';\r\nimport { NgModule } from '@angular\/core';\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgxGoogleAnalyticsModule, NgxGoogleAnalyticsRouterModule } from 'ngx-google-analytics';\r\n\r\nimport { AppRoutingModule } from '.\/app-routing.module';\r\nimport { AppComponent } from '.\/app.component';\r\nimport { NavBarComponent } from '.\/core\/nav-bar\/nav-bar.component';\r\n\r\n@NgModule({\r\n  declarations: &#91;\r\n    AppComponent,\r\n    NavBarComponent\r\n  ],\r\n  imports: &#91;\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    HttpClientModule,\r\n    NgxGoogleAnalyticsModule.forRoot('MEASUREMENT-ID'),\r\n    NgxGoogleAnalyticsRouterModule\r\n  ],\r\n  providers: &#91;],\r\n  bootstrap: &#91;AppComponent]\r\n})\r\nexport class AppModule { }<\/code><\/pre>\n\n\n\n<p><em><strong><span class=\"has-inline-color has-vivid-red-color\">PS: <\/span><\/strong>You can learn more about this package and all of it&#8217;s options, by reading the documentation\u00a0<a href=\"https:\/\/github.com\/maxandriani\/ngx-google-analytics#readme\">here<\/a><\/em><\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>Alternative <\/em>2<\/span><\/h4>\n\n\n\n<p>The first step is to go into the settings for your Google Analytics property and find the\u00a0<code>Global site tag<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-8.png\" alt=\"\" class=\"wp-image-2485\" width=\"717\" height=\"367\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-8.png 861w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-8-300x154.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-8-768x393.png 768w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/figure><\/div>\n\n\n\n<p>You want to copy the global site tag into the&nbsp;<code>head<\/code>&nbsp;of your&nbsp;<code>index.html<\/code>&nbsp;file.<\/p>\n\n\n\n<p>It should look sorta like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!doctype html>\r\n&lt;html lang=\"en\">\r\n&lt;head>\r\n  &lt;meta charset=\"utf-8\">\r\n  &lt;title>Angular Application&lt;\/title>\r\n  &lt;base href=\"\/\">\r\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n  &lt;link rel=\"icon\" type=\"image\/x-icon\" href=\"favicon.ico\">\r\n  &lt;!-- Global site tag (gtag.js) - Google Analytics -->\r\n  &lt;script async src=\"https:\/\/www.googletagmanager.com\/gtag\/js?id=MEASUREMENT-ID\">&lt;\/script>\r\n  &lt;script>\r\n    window.dataLayer = window.dataLayer || &#91;];\r\n    function gtag(){dataLayer.push(arguments);}\r\n    gtag('js', new Date());\r\n\r\n    gtag('config', 'MEASUREMENT-ID');\r\n  &lt;\/script>\r\n&lt;\/head>\r\n&lt;body>\r\n  &lt;app-root>&lt;\/app-root>\r\n&lt;\/body>\r\n&lt;\/html><\/code><\/pre>\n\n\n\n<p>This is a good start but again&nbsp;<strong>there is a problem<\/strong>. We&#8217;re not quite finished because Google Analytics is not notified when we route to a page inside our SPA.<\/p>\n\n\n\n<p>To fix that, we&#8217;ll subscribe to the Angular router inside the\u00a0<code>app.component.ts<\/code>\u00a0file and send the new routes to Google Analytics.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component } from '@angular\/core';\r\nimport { NavigationEnd, Router } from '@angular\/router';\r\n\r\ndeclare const gtag: Function;\r\n\r\n@Component({\r\n  selector: 'app-root',\r\n  templateUrl: '.\/app.component.html',\r\n  styleUrls: &#91;'.\/app.component.css']\r\n})\r\nexport class AppComponent {\r\n  constructor(public router: Router) {\r\n    this.router.events.subscribe((event) => {\r\n      if (event instanceof NavigationEnd) {\r\n        gtag('config', 'MEASUREMENT-ID', { 'page_path': event.urlAfterRedirects });\r\n      }      \r\n    })\r\n  }\r\n}<\/code><\/pre>\n\n\n\n<p>That^s it. We&#8217;ve installed Google Analytics and are now sending page views to the Google Analytics servers.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>How to use Google Analytics Data API?<\/h4>\n\n\n\n<h4>Requirements<\/h4>\n\n\n\n<p>We need three values to fetch the data:<\/p>\n\n\n\n<ul><li><code>GA_PROPERTY_ID<\/code><\/li><li><code>GA_CLIENT_EMAIL<\/code><\/li><li><code>GA_PRIVATE_KEY<\/code><\/li><\/ul>\n\n\n\n<h5><code><span class=\"has-inline-color has-vivid-cyan-blue-color\">GA_PROPERTY_ID<\/span><\/code><\/h5>\n\n\n\n<p>First, you need to get&nbsp;<code><strong>GA_PROPERTY_ID<\/strong><\/code>&nbsp;of the Google Analytics project from which you want to fetch the data.<\/p>\n\n\n\n<p>To get this you need to follow these steps:<\/p>\n\n\n\n<ul><li>Visit\u00a0<a href=\"https:\/\/analytics.google.com\/\">Google Analytics<\/a>.<\/li><li>Select Admin.<\/li><li>Select the Property.<\/li><li>Select Property Setting<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image.png\" alt=\"This image has an empty alt attribute; its file name is image.png\"\/><\/figure>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong><code>GA_CLIENT_EMAIL<\/code>\u00a0<\/strong><\/span>and\u00a0<code><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">GA_PRIVATE_KEY<\/span><\/strong><\/code><\/p>\n\n\n\n<p>To get\u00a0<code>GA_CLIENT_EMAIL<\/code>\u00a0and\u00a0<code>GA_PRIVATE_KEY<\/code>\u00a0you need to visit Google Analytics Data API&#8217;s\u00a0<a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/data\/v1\/quickstart-client-libraries\">documentation<\/a>\u00a0and then click on\u00a0<strong>Enable the Google Analytics Data API v1<\/strong>\u00a0button as shown in the image below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-1.png\" alt=\"\" class=\"wp-image-2477\" width=\"628\" height=\"104\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-1.png 894w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-300x50.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-1-768x127.png 768w\" sizes=\"(max-width: 628px) 100vw, 628px\" \/><\/figure><\/div>\n\n\n\n<p>And then you will be prompted to enter the name of the project. After entering the name click on the\u00a0<strong>Next<\/strong>\u00a0button<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-2.png\" alt=\"\" class=\"wp-image-2478\" width=\"624\" height=\"251\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-2.png 896w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-2-300x121.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-2-768x309.png 768w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure><\/div>\n\n\n\n<p>Then you will be prompted to download the credential file as JSON.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-3.png\" alt=\"\" class=\"wp-image-2479\" width=\"588\" height=\"313\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-3.png 849w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-3-300x160.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2023\/01\/image-3-768x409.png 768w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><\/figure><\/div>\n\n\n\n<p>After downloading that file. You will find&nbsp;<code>private_key<\/code>&nbsp;and&nbsp;<code>client_email<\/code>&nbsp;properties in that JSON file. Save these two in your&nbsp;<code>.env.local<\/code>.<\/p>\n\n\n\n<p>Now you have all the necessary information or keys.<\/p>\n\n\n\n<h4>Installing Dependency<\/h4>\n\n\n\n<p>To fetch the data you need to install a\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/@google-analytics\/data\">@google-analytics\/data<\/a>\u00a0package. You can do that by simply running the following command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm i @google-analytics\/data<\/code><\/pre>\n\n\n\n<h4>Using Google Analytics Data API in Project<\/h4>\n\n\n\n<p>Let assume we are using <a href=\"https:\/\/nextjs.org\/\">Next.js<\/a>, so we will use <a href=\"https:\/\/nextjs.org\/docs\/api-routes\/introduction\">Next.js API Routes<\/a><\/p>\n\n\n\n<p>Let&#8221;s create an API route\u00a0<code>pages\/api\/ga.ts<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { NextApiRequest, NextApiResponse } from \"next\";\r\nimport { BetaAnalyticsDataClient } from \"@google-analytics\/data\";\r\n\r\n\/\/ ? Setting PropertyId\r\nconst propertyId = process.env.GA_PROPERTY_ID;\r\n\r\nconst analyticsDataClient = new BetaAnalyticsDataClient({\r\n  credentials: {\r\n    client_email: process.env.GA_CLIENT_EMAIL,\r\n    private_key: process.env.GA_PRIVATE_KEY?.replace(\/\\n\/gm, \"\\n\"), \/\/ replacing is necessary\r\n  },\r\n});\r\n\r\nexport default async function handler(\r\n  _req: NextApiRequest,\r\n  res: NextApiResponse\r\n) {\r\n  \/\/ ? Running a simple report\r\n  const &#91;response] = await analyticsDataClient.runReport({\r\n    property: `properties\/${propertyId}`,\r\n    dateRanges: &#91;\r\n      {\r\n        startDate: `7daysAgo`, \/\/?  e.g. \"7daysAgo\" or \"30daysAgo\"\r\n        endDate: \"today\",\r\n      },\r\n    ],\r\n    dimensions: &#91;\r\n      {\r\n        name: \"year\", \/\/ data will be year wise\r\n      },\r\n    ],\r\n    metrics: &#91;\r\n      {\r\n        name: \"activeUsers\", \/\/ it returs the active users\r\n      },\r\n    ],\r\n  });\r\n\r\n  \/\/ Returning the respose.\r\n  return res.status(200).json({\r\n    response,\r\n  });\r\n}<\/code><\/pre>\n\n\n\n<p>This is all you need to fetch the data. and It will return the response.<\/p>\n\n\n\n<p><strong><em><span class=\"has-inline-color has-vivid-red-color\">PS:<\/span><\/em><\/strong> <em>You can do the same thing with different frameworks.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/danielk.tech\/\">https:\/\/danielk.tech\/<\/a> <\/p>\n\n\n\n<p><a href=\"https:\/\/dev.to\/j471n\/how-to-use-google-analytics-data-api-2133\">https:\/\/dev.to\/j471n\/how-to-use-google-analytics-data-api-2133<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When someone asks about Tracking web pages for analysis, generally Google Analytics product by Google comes up first in your mind, Right? Google Analytics collects data about your web presence from various sources. There are three primary sources of this data: User activities on your website or app Any CRM and external data sources you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,26,5],"tags":[647,28,648,313],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2475"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2475"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2475\/revisions"}],"predecessor-version":[{"id":2488,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2475\/revisions\/2488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}