Skip to content
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

feat(other): Add analytics support #7899

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(

Check warning on line 13 in packages/analytics/lib/web/RNFBAnalyticsModule.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics/lib/web/RNFBAnalyticsModule.js#L13

Added line #L13 was not covered by tests
'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);

Check warning on line 20 in packages/analytics/lib/web/RNFBAnalyticsModule.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics/lib/web/RNFBAnalyticsModule.js#L20

Added line #L20 was not covered by tests
}
}
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 });

Check warning on line 78 in packages/analytics/lib/web/RNFBAnalyticsModule.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics/lib/web/RNFBAnalyticsModule.js#L78

Added line #L78 was not covered by tests
}
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
Loading