问题
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