@@ -3,20 +3,14 @@ import GoogleAnalyticsContext, { GoogleAnalyticsState } from './GoogleAnalyticsC
33
44export interface GoogleAnalyticsProviderProps {
55 children : React . ReactNode ;
6- /** The client ID of your project in the developers console. */
7- clientId ?: string ;
86 /**
9- * If you already have a valid access token, you can pass it
10- * to the authorize method directly and the user will not be
11- * prompted to authorize.
7+ * Access token to authorize to the analytics service.
128 */
139 accessToken ?: string ;
14- /** Firing when a user failed to authenticate using the sign in button */
15- onAuthError ?: ( ) => { } ;
16- /** Firing when a user authenticated using the sign in button */
17- onAuthSuccess ?: ( ) => { } ;
1810 /** Firing after the gapi script loaded and ready to use **/
19- onReady ?: ( ) => { } ;
11+ onReady ?: ( ) => void ;
12+ /** Firing after the authenitcated to analytics service using access token **/
13+ onAuthenticated ?: ( ) => void ;
2014 /** A list of Google API auth scopes that your application is requesting **/
2115 scopes ?: string [ ] ;
2216 /**
@@ -41,10 +35,10 @@ export interface GoogleAnalyticsProviderProps {
4135const GoogleAnalyticsProvider : React . FC < GoogleAnalyticsProviderProps > = (
4236 props : GoogleAnalyticsProviderProps
4337) : React . ReactElement => {
44- // For frontend authentication
45- const [ authButton , setAuthButton ] = React . useState < null | HTMLElement > ( null ) ;
4638 // Internal state
4739 const [ gaState , setGaState ] = React . useState < GoogleAnalyticsState > ( 'INITIALIZED' ) ;
40+
41+ const { onReady, onAuthenticated } = props ;
4842 // Importing the google platform script and
4943 React . useEffect ( ( ) => {
5044 if ( document . getElementById ( 'gaScript' ) == null ) {
@@ -75,6 +69,7 @@ const GoogleAnalyticsProvider: React.FC<GoogleAnalyticsProviderProps> = (
7569 if ( gaState == 'PROCESSING' ) {
7670 window . gapi . analytics . ready ( ( ) => {
7771 setGaState ( 'READY' ) ;
72+ onReady != null && onReady ( ) ;
7873 } ) ;
7974 }
8075 } , [ gaState ] ) ;
@@ -93,33 +88,12 @@ const GoogleAnalyticsProvider: React.FC<GoogleAnalyticsProviderProps> = (
9388 overwriteDefaultScopes : props . overwriteDefaultScopes
9489 } ) ;
9590 setGaState ( 'AUTH_SUCCESS' ) ;
96- } else if ( props . clientId && authButton ) {
97- // button authorization
98- setGaState ( 'AUTHENTICATING' ) ;
99- gapi . analytics . auth . authorize ( {
100- clientid : props . clientId ,
101- container : authButton ,
102- userInfoLabel : props . userInfoLabel ,
103- scopes : props . scopes ,
104- overwriteDefaultScopes : props . overwriteDefaultScopes
105- } ) ;
106-
107- gapi . analytics . auth . on ( 'signIn' , ( ) => {
108- setGaState ( 'AUTH_SUCCESS' ) ;
109- } ) ;
110-
111- gapi . analytics . auth . on ( 'signOut' , ( ) => {
112- setGaState ( 'SIGNOUT' ) ;
113- } ) ;
114-
115- gapi . analytics . auth . on ( 'error' , ( ) => {
116- setGaState ( 'AUTH_ERROR' ) ;
117- } ) ;
91+ onAuthenticated != null && onAuthenticated ( ) ;
11892 }
11993 }
120- } , [ props . accessToken , props . clientId , gaState , authButton ] ) ;
94+ } , [ props . accessToken , gaState ] ) ;
12195 return (
122- < GoogleAnalyticsContext . Provider value = { [ gaState , setAuthButton ] } >
96+ < GoogleAnalyticsContext . Provider value = { gaState } >
12397 < div > { props . children } </ div >
12498 </ GoogleAnalyticsContext . Provider >
12599 ) ;
0 commit comments