-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(other): Add analytics support (#7899)
- Loading branch information
Showing
11 changed files
with
412 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// No-op for android. | ||
export default {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// No-op for ios. | ||
export default {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { getApp } from '@react-native-firebase/app/lib/internal/web/firebaseApp'; | ||
import { guard } from '@react-native-firebase/app/lib/internal/web/utils'; | ||
|
||
import { AnalyticsApi } from './api'; | ||
|
||
let analyticsInstances = {}; | ||
|
||
function getAnalyticsApi() { | ||
const app = getApp('[DEFAULT]'); | ||
const measurementId = app.options.measurementId; | ||
if (!measurementId) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'No measurement id found for Firebase Analytics. Analytics will be unavailable. Have you set gaTrackingId in your Firebase config?', | ||
); | ||
} | ||
if (!analyticsInstances[measurementId]) { | ||
analyticsInstances[measurementId] = new AnalyticsApi('[DEFAULT]', measurementId); | ||
if (global.RNFBDebug) { | ||
analyticsInstances[measurementId].setDebug(true); | ||
} | ||
} | ||
return analyticsInstances[measurementId]; | ||
} | ||
|
||
/** | ||
* This is a 'NativeModule' for the web platform. | ||
* Methods here are identical to the ones found in | ||
* the native android/ios modules e.g. `@ReactMethod` annotated | ||
* java methods on Android. | ||
*/ | ||
export default { | ||
logEvent(name, params) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.logEvent(name, params); | ||
return null; | ||
}); | ||
}, | ||
|
||
setUserId(userId) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.setUserId(userId); | ||
return null; | ||
}); | ||
}, | ||
|
||
setUserProperty(key, value) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.setUserProperty(key, value); | ||
return null; | ||
}); | ||
}, | ||
|
||
setUserProperties(properties) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.setUserProperties(properties); | ||
return null; | ||
}); | ||
}, | ||
|
||
setDefaultEventParameters(params) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.setDefaultEventParameters(params); | ||
return null; | ||
}); | ||
}, | ||
|
||
setConsent(consent) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
// TODO currently we only support ad_personalization | ||
if (consent && consent.ad_personalization) { | ||
api.setConsent({ ad_personalization: consent.ad_personalization }); | ||
} | ||
return null; | ||
}); | ||
}, | ||
|
||
setAnalyticsCollectionEnabled(enabled) { | ||
return guard(async () => { | ||
const api = getAnalyticsApi('[DEFAULT]'); | ||
api.setAnalyticsCollectionEnabled(enabled); | ||
return null; | ||
}); | ||
}, | ||
|
||
resetAnalyticsData() { | ||
// Unsupported for web. | ||
return guard(async () => { | ||
return null; | ||
}); | ||
}, | ||
|
||
setSessionTimeoutDuration() { | ||
// Unsupported for web. | ||
return guard(async () => { | ||
return null; | ||
}); | ||
}, | ||
|
||
getAppInstanceId() { | ||
// Unsupported for web. | ||
return guard(async () => { | ||
return null; | ||
}); | ||
}, | ||
|
||
getSessionId() { | ||
// Unsupported for web. | ||
return guard(async () => { | ||
return null; | ||
}); | ||
}, | ||
}; |
Oops, something went wrong.