Skip to content

Commit

Permalink
fix: solved some GA problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Jul 6, 2023
1 parent f7cfa70 commit 0d3d9f8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-beers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"xlog": patch
---

Only enable GA in production envrionment.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-vans-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"xlog": patch
---

Fixed the problem of inaccurate recording of reading time.
5 changes: 5 additions & 0 deletions .changeset/smart-knives-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"xlog": minor
---

Fixed crush error when go back on iOS.
2 changes: 1 addition & 1 deletion app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default (_: ConfigContext): ExpoConfig => {
},
ios: {
deploymentTarget: "13.0",
useFrameworks: "static",
},
},
],
Expand All @@ -64,6 +63,7 @@ export default (_: ConfigContext): ExpoConfig => {
"expo-localization",
"sentry-expo",
"@react-native-firebase/app",
"./plugins/with-react-native-firebase.js",
],
splash: {
image: "./assets/splash.png",
Expand Down
50 changes: 50 additions & 0 deletions plugins/with-react-native-firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");
const path = require("path");

const generateCode = require("@expo/config-plugins/build/utils/generateCode");
const configPlugins = require("expo/config-plugins");

const code = ` pod 'Firebase', :modular_headers => true
pod 'nanopb', :modular_headers => true
pod 'FirebaseSessions', :modular_headers => true
pod 'FirebaseCore', :modular_headers => true
pod 'FirebaseCoreExtension', :modular_headers => true
pod 'FirebaseInstallations', :modular_headers => true
pod 'GoogleDataTransport', :modular_headers => true
pod 'GoogleUtilities', :modular_headers => true
$RNFirebaseAsStaticFramework = true`;

const withReactNativeFirebase = (config) => {
return configPlugins.withDangerousMod(config, [
"ios",
async (config) => {
const filePath = path.join(
config.modRequest.platformProjectRoot,
"Podfile",
);
const contents = fs.readFileSync(filePath, "utf-8");

const addCode = generateCode.mergeContents({
tag: "withReactNativeFirebase",
src: contents,
newSrc: code,
anchor: /\s*get_default_flags\(\)/i,
offset: 2,
comment: "#",
});

if (!addCode.didMerge) {
console.error(
"ERROR: Cannot add withReactNativeFirebase to the project's ios/Podfile because it's malformed.",
);
return config;
}

fs.writeFileSync(filePath, addCode.contents);

return config;
},
]);
};

module.exports = withReactNativeFirebase;
9 changes: 5 additions & 4 deletions src/hooks/ga/use-ga-with-page-stay-time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from "react";
import { useEffect, useRef } from "react";
import { Platform } from "react-native";

import { GA } from "@/utils/GA";

Expand All @@ -13,7 +14,7 @@ interface Time {
}

const useGAWithPageStayTime = (event: Event) => {
const startTime = Date.now();
const startTime = useRef(Date.now());

const formatTime = (ms: number): string => {
const seconds = Math.floor(ms / 1000);
Expand All @@ -29,7 +30,7 @@ const useGAWithPageStayTime = (event: Event) => {

useEffect(() => {
return () => {
const stayTime = Date.now() - startTime;
const stayTime = Date.now() - startTime.current;
const { page_name, params } = event;
const formattedTime = formatTime(stayTime);
GA.logEvent("page_stay_time", {
Expand All @@ -39,7 +40,7 @@ const useGAWithPageStayTime = (event: Event) => {
stay_time_formatted: formattedTime,
});
};
}, [event, startTime]);
}, []);
};

export default useGAWithPageStayTime;
19 changes: 4 additions & 15 deletions src/utils/GA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ import analytics from "@react-native-firebase/analytics";

import { IS_PROD } from "@/constants";

const fakerFn = (...args) => {
// eslint-disable-next-line no-console
console.log("[GA]: ", args.map(arg => JSON.stringify(arg)).join(", "));
return Promise.resolve();
};
const analyticsInstance = analytics();

export const GA = IS_PROD
? analytics()
: {
logEvent: fakerFn,
logShare: fakerFn,
logLogin: fakerFn,
logSearch: fakerFn,
logSelectItem: fakerFn,
logScreenView: fakerFn,
};
analyticsInstance.setAnalyticsCollectionEnabled(IS_PROD);

export const GA = analyticsInstance;

0 comments on commit 0d3d9f8

Please sign in to comment.