Skip to content

Commit

Permalink
feat(other): Add analytics support (#7899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar authored Jul 15, 2024
1 parent a41e556 commit cbdf9ec
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/analytics/e2e/analytics.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
*/

describe('analytics() modular', function () {
describe('firebase v8 compatibility', function () {
describe('namespace', function () {});
beforeEach(async function () {
await firebase.analytics().logEvent('screen_view');
});

describe('firebase v8 compatibility', function () {
describe('logEvent()', function () {
it('log an event without parameters', async function () {
await firebase.analytics().logEvent('invertase_event');
Expand Down Expand Up @@ -56,6 +58,9 @@ describe('analytics() modular', function () {
});

it('returns a non empty session ID', async function () {
if (Platform.other) {
this.skip();
}
let sessionId = await firebase.analytics().getSessionId();
// On iOS it can take ~ 3 minutes for the session ID to be generated
// Otherwise, `Analytics uninitialized` error will be thrown
Expand Down Expand Up @@ -584,6 +589,9 @@ describe('analytics() modular', function () {
});

it('returns a non empty session ID', async function () {
if (Platform.other) {
this.skip();
}
const { getAnalytics, getSessionId } = analyticsModular;
let sessionId = await getSessionId(getAnalytics());
// On iOS it can take ~ 3 minutes for the session ID to be generated
Expand Down
5 changes: 5 additions & 0 deletions packages/analytics/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import {
FirebaseModule,
getFirebaseRoot,
} from '@react-native-firebase/app/lib/internal';
import { setReactNativeModule } from '@react-native-firebase/app/lib/internal/nativeModule';
import { isBoolean } from '@react-native-firebase/app/lib/common';

import fallBackModule from './web/RNFBAnalyticsModule';
import version from './version';
import * as structs from './structs';

Expand Down Expand Up @@ -828,3 +830,6 @@ export default createModuleNamespace({
// analytics().logEvent(...);
// firebase.analytics().logEvent(...);
export const firebase = getFirebaseRoot();

// Register the interop module for non-native platforms.
setReactNativeModule(nativeModuleName, fallBackModule);
2 changes: 2 additions & 0 deletions packages/analytics/lib/web/RNFBAnalyticsModule.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// No-op for android.
export default {};
2 changes: 2 additions & 0 deletions packages/analytics/lib/web/RNFBAnalyticsModule.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// No-op for ios.
export default {};
119 changes: 119 additions & 0 deletions packages/analytics/lib/web/RNFBAnalyticsModule.js
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;
});
},
};
Loading

0 comments on commit cbdf9ec

Please sign in to comment.