@devboldly/react-use-google-analytics-embed-api
Advanced tools
Comparing version 0.0.28 to 0.0.29
import { GoogleAnalyticsEmbedAPILoader } from './useAnalyticsApi'; | ||
/** | ||
* See: https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#auth-options | ||
*/ | ||
export interface AuthorizeOptions { | ||
@@ -26,2 +29,2 @@ /** The client ID of your project in the [developers console](https://console.developers.google.com/project). */ | ||
} | ||
export declare const useAuthorize: (gapiLoader: GoogleAnalyticsEmbedAPILoader, options: AuthorizeOptions, onSignIn?: (() => void) | undefined) => boolean; | ||
export declare const useAuthorize: (gapiLoader: GoogleAnalyticsEmbedAPILoader, options: AuthorizeOptions, onSignIn?: (() => void) | undefined) => [boolean, boolean]; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -18,34 +29,26 @@ if (mod && mod.__esModule) return mod; | ||
exports.useAuthorize = function (gapiLoader, options, onSignIn) { | ||
var refreshedRequiredKeyName = 'refreshRequired'; | ||
// This hook will pull the global value from the previous render only. | ||
// This is important because we can only call Google's authorize() | ||
// function once. After that, it refuses to populate the container it's given. :( | ||
// So on the second render and after, we want to indicate that a refresh is required. | ||
var _a = react_use_window_global_1.useWindowGlobal(global_namespace_1.default, refreshedRequiredKeyName, false), refreshRequired = _a[1]; | ||
var _a = react_use_window_global_1.useWindowGlobal(global_namespace_1.default, 'authorizeCalled', false), authorizedCalledLoading = _a[0], authorizedCalled = _a[1], setAuthorizeCalled = _a[2]; | ||
var _b = react_use_window_global_1.useWindowGlobal(global_namespace_1.default, useAnalyticsApi_1.authorizedKeyName, false), authorized = _b[1], setAuthorized = _b[2]; | ||
React.useEffect(function () { | ||
var signInHandler = function () { | ||
if (!authorized) { | ||
setAuthorized(true); | ||
} | ||
if (typeof onSignIn !== 'undefined') { | ||
onSignIn(); | ||
} | ||
}; | ||
var win = window; | ||
// If a refresh isn't required (since authorize can only be called once) | ||
if (!win[global_namespace_1.default][refreshedRequiredKeyName]) { | ||
if (!authorizedCalledLoading && !authorizedCalled) { | ||
var signInHandler = function () { | ||
if (!authorized) { | ||
setAuthorized(true); | ||
} | ||
if (typeof onSignIn !== 'undefined') { | ||
onSignIn(); | ||
} | ||
}; | ||
if (gapiLoader.ready && gapiLoader.gapi) { | ||
gapiLoader.gapi.analytics.auth.on('signIn', signInHandler); | ||
gapiLoader.gapi.analytics.auth.authorize({ | ||
container: options.container, | ||
clientid: options.clientId, | ||
}); | ||
// Set this directly to prevent a rerender. | ||
// Authorize can only be called once so we need to track whether a refresh is required. | ||
win[global_namespace_1.default][refreshedRequiredKeyName] = true; | ||
gapiLoader.gapi.analytics.auth.authorize(__assign({ | ||
// There seems to be a typo in the official spec docs (clientId vs clientid) | ||
clientid: options.clientId }, options)); | ||
setAuthorizeCalled(true); | ||
} | ||
} | ||
}); | ||
return !!refreshRequired; | ||
var loading = authorizedCalledLoading; | ||
var called = !!authorizedCalled; | ||
return [loading, called]; | ||
}; |
{ | ||
"name": "@devboldly/react-use-google-analytics-embed-api", | ||
"version": "0.0.28", | ||
"version": "0.0.29", | ||
"author": "Dev Boldly <devboldly@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "React hook to async load the Google Analytics Embed API.", |
89396
1990