-
Notifications
You must be signed in to change notification settings - Fork 928
Issue 2393 - Add environment check to Analytics Module #3165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
0843a6e
0c426c3
fd0c0a3
9512b48
b5c7b78
e10388c
3ac0fe3
a377c68
5932e87
541bb0c
0090a18
8e979a3
7d419f1
08e4a6f
571e514
b22c1fc
dc75ec1
64d8be7
099816a
c8cc946
d16cbbd
ad09146
948a5da
a1103e1
f341f94
111d792
c9f137b
1fa549c
9d12700
2b00fc1
c5e2596
e30d0ec
d7b3359
12852f9
e9eb751
e00edfa
68387e6
17973b6
44e5aeb
1321327
6debf5d
21a4f7c
db750f8
4dec8d9
9d46212
51303b0
64df7e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ declare global { | |
* Type constant for Firebase Analytics. | ||
*/ | ||
const ANALYTICS_TYPE = 'analytics'; | ||
const NAMESPACE_EXPORTS = { | ||
isSupported | ||
}; | ||
export function registerAnalytics(instance: _FirebaseNamespace): void { | ||
instance.INTERNAL.registerComponent( | ||
new Component( | ||
|
@@ -56,12 +59,15 @@ export function registerAnalytics(instance: _FirebaseNamespace): void { | |
.getProvider('installations') | ||
.getImmediate(); | ||
|
||
validateBrowserContext(); | ||
|
||
return factory(app, installations); | ||
}, | ||
ComponentType.PUBLIC | ||
).setServiceProps({ | ||
settings, | ||
EventName | ||
EventName, | ||
NAMESPACE_EXPORTS | ||
}) | ||
); | ||
|
||
|
@@ -97,8 +103,52 @@ registerAnalytics(firebase as _FirebaseNamespace); | |
declare module '@firebase/app-types' { | ||
interface FirebaseNamespace { | ||
analytics(app?: FirebaseApp): FirebaseAnalytics; | ||
isSupported(): boolean; | ||
} | ||
interface FirebaseApp { | ||
analytics(): FirebaseAnalytics; | ||
} | ||
} | ||
function validateBrowserContext(): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably add jsdoc style comment explaining what this function does. |
||
if ('indexedDB' in window && indexedDB !== null && navigator.cookieEnabled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the |
||
try { | ||
let preExist: boolean = true; | ||
const DUMMYDBNAME = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
'a-dummy-database-for-testing-browser-context-firebase'; | ||
const request = window.indexedDB.open(DUMMYDBNAME); | ||
request.onsuccess = () => { | ||
//console.log('successfully opend dummy indexedDB'); | ||
request.result.close(); | ||
// delete database only when it doesn't pre-exist | ||
if (!preExist) { | ||
//console.log("deleting database"); | ||
window.indexedDB.deleteDatabase(DUMMYDBNAME); | ||
} | ||
}; | ||
request.onupgradeneeded = () => { | ||
preExist = false; | ||
//console.log('database needs to be upgraded'); | ||
}; | ||
|
||
request.onerror = () => { | ||
throw ERROR_FACTORY.create(AnalyticsError.INVALID_INDEXED_DB_CONTEXT, { | ||
errorInfo: request.error!.message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any possibility there might not be an error or a message? would it be safer to go with |
||
}); | ||
}; | ||
} catch (error) { | ||
throw ERROR_FACTORY.create(AnalyticsError.INVALID_INDEXED_DB_CONTEXT, { | ||
errorInfo: error | ||
}); | ||
} | ||
} else { | ||
throw ERROR_FACTORY.create(AnalyticsError.INDEXED_DB_UNSUPPORTED); | ||
} | ||
} | ||
function isSupported(): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably have jsdoc style comment explaining what this function does. |
||
try { | ||
validateBrowserContext(); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,9 @@ export const enum AnalyticsError { | |
NO_GA_ID = 'no-ga-id', | ||
ALREADY_EXISTS = 'already-exists', | ||
ALREADY_INITIALIZED = 'already-initialized', | ||
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed' | ||
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed', | ||
INDEXED_DB_UNSUPPORTED = 'indexedDB-unsupported', | ||
INVALID_INDEXED_DB_CONTEXT = 'invalid-indexedDB-context' | ||
} | ||
|
||
const ERRORS: ErrorMap<AnalyticsError> = { | ||
|
@@ -39,12 +41,17 @@ const ERRORS: ErrorMap<AnalyticsError> = { | |
'settings() must be called before initializing any Analytics instance' + | ||
'or it will have no effect.', | ||
[AnalyticsError.INTEROP_COMPONENT_REG_FAILED]: | ||
'Firebase Analytics Interop Component failed to instantiate' | ||
'Firebase Analytics Interop Component failed to instantiate', | ||
[AnalyticsError.INDEXED_DB_UNSUPPORTED]: | ||
'IndexedDB is not supported by current browswer', | ||
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: | ||
"Environment doesn't support IndexedDB functionality with error message: {$errorInfo}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think we need the words |
||
}; | ||
|
||
interface ErrorParams { | ||
[AnalyticsError.ALREADY_EXISTS]: { id: string }; | ||
[AnalyticsError.INTEROP_COMPONENT_REG_FAILED]: { reason: Error }; | ||
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: { errorInfo: string }; | ||
} | ||
|
||
export const ERROR_FACTORY = new ErrorFactory<AnalyticsError, ErrorParams>( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5083,6 +5083,7 @@ declare namespace firebase.analytics { | |
id?: string; | ||
name?: string; | ||
} | ||
function isSupported(): boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will need a comment here to document what this does. Comments in this file are the source of our official documentation, see other methods/properties for format. |
||
} | ||
|
||
declare namespace firebase.auth.Auth { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is copied from messaging, but I don't think it's necessary here. In messaging, this was the top-level object passed to
setServiceProps()
. Here, the top level object is a literal that currently containssettings
andEventName
, so you're adding another key namedNAMESPACE_EXPORTS
which contains this nested object that containsisSupported()
. Have you manually tested calling theisSupported()
static method? Did this work?