mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-10-31 18:19:43 +00:00
Fix Firebase Analytics setup and disable ad ID
This commit is contained in:
1 parent
a558f5e67e
commit
12f312421b
18 files changed
+3791
-1422
No files matched your search
@@ -1,31 +1,77 @@
|
||||
import { getApp } from "@react-native-firebase/app";
|
||||
import {
|
||||
getAnalytics,
|
||||
logEvent as firebaseLogEvent,
|
||||
setUserId as firebaseSetUserId,
|
||||
setUserProperty,
|
||||
setAnalyticsCollectionEnabled,
|
||||
resetAnalyticsData,
|
||||
FirebaseAnalyticsTypes,
|
||||
} from "@react-native-firebase/analytics";
|
||||
|
||||
let isInitialized = false;
|
||||
let analyticsInstance: FirebaseAnalyticsTypes.Module | null = null;
|
||||
|
||||
function getAnalyticsInstance(): FirebaseAnalyticsTypes.Module | null {
|
||||
if (analyticsInstance) {
|
||||
return analyticsInstance;
|
||||
}
|
||||
|
||||
try {
|
||||
const app = getApp();
|
||||
analyticsInstance = getAnalytics(app);
|
||||
return analyticsInstance;
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to get analytics instance:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
import { Platform } from "react-native";
|
||||
import Constants from "expo-constants";
|
||||
// Import default export to ensure namespace registration runs
|
||||
import analytics, {
|
||||
getAnalytics,
|
||||
logEvent as firebaseLogEvent,
|
||||
setUserId as firebaseSetUserId,
|
||||
setUserProperty,
|
||||
setAnalyticsCollectionEnabled,
|
||||
resetAnalyticsData,
|
||||
FirebaseAnalyticsTypes,
|
||||
} from "@react-native-firebase/analytics";
|
||||
|
||||
// Force the analytics module to be retained (prevents tree-shaking)
|
||||
const _analyticsModule = analytics;
|
||||
|
||||
let isInitialized = false;
|
||||
let analyticsInstance: FirebaseAnalyticsTypes.Module | null = null;
|
||||
let analyticsUnavailable = false;
|
||||
let analyticsUnavailableLogged = false;
|
||||
|
||||
function getErrorMessage(error: unknown): string {
|
||||
if (!error) return "";
|
||||
if (typeof error === "string") return error;
|
||||
if (error instanceof Error) return error.message;
|
||||
if (typeof (error as { message?: unknown }).message === "string") {
|
||||
return (error as { message?: string }).message || "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function isAnalyticsSupported(): boolean {
|
||||
if (Platform.OS === "web") return false;
|
||||
if (Constants.appOwnership === "expo") return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function logAnalyticsUnavailableOnce(reason: string) {
|
||||
if (analyticsUnavailableLogged) return;
|
||||
analyticsUnavailableLogged = true;
|
||||
if (__DEV__) console.log("[Analytics] " + reason);
|
||||
}
|
||||
|
||||
function getAnalyticsInstance(): FirebaseAnalyticsTypes.Module | null {
|
||||
if (analyticsUnavailable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isAnalyticsSupported()) {
|
||||
analyticsUnavailable = true;
|
||||
logAnalyticsUnavailableOnce("Skipping Firebase Analytics on Expo Go or web.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (analyticsInstance) {
|
||||
return analyticsInstance;
|
||||
}
|
||||
|
||||
try {
|
||||
analyticsInstance = getAnalytics();
|
||||
return analyticsInstance;
|
||||
} catch (error) {
|
||||
const message = getErrorMessage(error);
|
||||
if (message.includes("module could not be found")) {
|
||||
analyticsUnavailable = true;
|
||||
logAnalyticsUnavailableOnce(
|
||||
"Firebase Analytics native module missing. Rebuild with a dev client or native app."
|
||||
);
|
||||
return null;
|
||||
}
|
||||
console.warn("[Analytics] Failed to get analytics instance:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function initAnalytics() {
|
||||
if (isInitialized) {
|
||||
@@ -38,12 +84,12 @@ export async function initAnalytics() {
|
||||
}
|
||||
|
||||
try {
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
await setAnalyticsCollectionEnabled(analytics, true);
|
||||
isInitialized = true;
|
||||
console.log("[Analytics] Firebase Analytics initialized");
|
||||
}
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
await setAnalyticsCollectionEnabled(analytics, true);
|
||||
isInitialized = true;
|
||||
console.log("[Analytics] Firebase Analytics initialized");
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to initialize Firebase Analytics:", error);
|
||||
}
|
||||
@@ -55,41 +101,41 @@ export function logEvent(
|
||||
params?: Record<string, string | number | boolean>
|
||||
) {
|
||||
try {
|
||||
if (__DEV__) console.log("[Analytics] Event:", name, params);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseLogEvent(analytics, name, params);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to log event:", error);
|
||||
}
|
||||
if (__DEV__) console.log("[Analytics] Event:", name, params);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseLogEvent(analytics, name, params);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to log event:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Set user ID for tracking
|
||||
export function setUserId(userId: string) {
|
||||
try {
|
||||
if (__DEV__) console.log("[Analytics] Set user ID:", userId);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseSetUserId(analytics, userId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to set user ID:", error);
|
||||
}
|
||||
if (__DEV__) console.log("[Analytics] Set user ID:", userId);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseSetUserId(analytics, userId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to set user ID:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear user ID on logout
|
||||
export function clearUserId() {
|
||||
try {
|
||||
if (__DEV__) console.log("[Analytics] Cleared user ID");
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseSetUserId(analytics, null);
|
||||
resetAnalyticsData(analytics);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to clear user ID:", error);
|
||||
}
|
||||
if (__DEV__) console.log("[Analytics] Cleared user ID");
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseSetUserId(analytics, null);
|
||||
resetAnalyticsData(analytics);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to clear user ID:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Set user properties
|
||||
@@ -97,29 +143,29 @@ export function setUserProperties(
|
||||
properties: Record<string, string | null>
|
||||
) {
|
||||
try {
|
||||
if (__DEV__) console.log("[Analytics] Set user properties:", properties);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
// Firebase Analytics requires setting user properties one at a time
|
||||
for (const [key, value] of Object.entries(properties)) {
|
||||
setUserProperty(analytics, key, value);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to set user properties:", error);
|
||||
}
|
||||
if (__DEV__) console.log("[Analytics] Set user properties:", properties);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
// Firebase Analytics requires setting user properties one at a time
|
||||
for (const [key, value] of Object.entries(properties)) {
|
||||
setUserProperty(analytics, key, value);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to set user properties:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Log screen view
|
||||
export function logScreenView(screenName: string, screenClass?: string) {
|
||||
try {
|
||||
if (__DEV__) console.log("[Analytics] Screen view:", screenName);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseLogEvent(analytics, "screen_view", {
|
||||
screen_name: screenName,
|
||||
screen_class: screenClass ?? screenName,
|
||||
});
|
||||
if (__DEV__) console.log("[Analytics] Screen view:", screenName);
|
||||
const analytics = getAnalyticsInstance();
|
||||
if (analytics) {
|
||||
firebaseLogEvent(analytics, "screen_view", {
|
||||
screen_name: screenName,
|
||||
screen_class: screenClass ?? screenName,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Analytics] Failed to log screen view:", error);
|
||||
|
||||
Reference in new issue
Block a user