Any pointers on using AngularFire2 With GAPI to Access Google Analytics Reporting

微笑、不失礼 提交于 2019-12-12 01:37:14

问题


Angular: Angular2 RC4

Firebase: Firebase3 (latest)

AngularFire: AngularFire2 (latest)

I'm looking at trying to access the Google Analytics Reporting v4 API. Something like this. But using Angular2 goodness.

Has anyone had any luck with doing this? I've googled, but it doesn't seem to be that popular of a topic. It doesn't help that Angular2 is still pretty new, so there's that. Does anyone have any idea? I'm sure it's a thing. I think?

I've got authentication working with AngularFire2 and Firebase, that's a first step. ;)


回答1:


UPDATE 2018

Someone added DefinitelyTyped/types/gapi.client.analytics for the Google Analytics Client API V3. Following this link there's a README with instructions on how to install it and use it.

ORIGINAL ANSWER

Back in 2016 this was my way of implementing it:

src/index.html

In the index.html file of your app you need to add this in the <head> section:

<script src="https://apis.google.com/js/platform.js" async defer></script>

typings/browser/ambient/gapi/

You need to add the following namespaces to your typings:

  • DefinitelyTyped/types/gapi
  • DefinitelyTyped/types/gapi.auth2
  • DefinitelyTyped/types/gapi.analytics

src/app/+login/login.component.ts

This is the file of my component, here you need to use the ngAfterViewInit() to use the gapi an get the auth.

As an example, this is my auth function where I also transform the API callback into a promise:

private authorize() : Promise<any> {
    var authCallback;
    let authData = {
        client_id: 'your-client-id.apps.googleusercontent.com',
        scope: ['https://www.googleapis.com/auth/analytics.readonly'],
        immediate: true
    };

    let p = new Promise((resolve,reject) => {
      authCallback = response => response.error? reject(response) : resolve(response);
    });

    gapi.auth.authorize(authData, authCallback);

    return p;
}


来源:https://stackoverflow.com/questions/38711127/any-pointers-on-using-angularfire2-with-gapi-to-access-google-analytics-reportin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!