Google analytics embedded API not working with Google Analytics Reporting API (v4)

Viewed 396

I want to show google analytics data to my client site and in some reason gapi won't recognize reporting api v4. Can someone help me with this?

Home.components.ts

  constructor(
    private credentialService: CredentialsService,
    private router: Router,
    public gapiService: GoogleApiService
  ) {}

  ngOnInit() {
    this.gapiService.onLoad().subscribe( () => {
      const VIEW_ID = '216494010';

      function queryReports() {
        gapi.client.request({
          path: '/v4/reports:batchGet',
          root: 'https://analyticsreporting.googleapis.com/',
          method: 'POST',
          body: {
            reportRequests: [
              {
                viewId: VIEW_ID,
                dateRanges: [
                  {
                    startDate: '7daysAgo',
                    endDate: 'today'
                  }
                ],
                metrics: [
                  {
                    expression: 'ga:sessions'
                  }
                ]
              }
            ]
          }
        }).then(displayResults, console.error.bind(console));
      }

      function displayResults(response: any) {
        const formattedJson = JSON.stringify(response.result, null, 2);
        // document.getElementById('query-output').value = formattedJson;
        console.log(response)
      }
    })
  }

Home.module.ts enter image description here

0 Answers
Related