Skip to content

Commit 8c7f87c

Browse files
kevmo314claude
andauthored
Fix Firebase Analytics warning in Node.js environments (#296)
Co-authored-by: Claude <[email protected]>
1 parent 0919317 commit 8c7f87c

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rtirl/api",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"main": "lib/index.js",
55
"module": "lib/index.mjs",
66
"browser": "lib/index.min.js",

api/src/index.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { getAnalytics, logEvent } from "firebase/analytics";
1+
import {
2+
Analytics,
3+
getAnalytics,
4+
isSupported,
5+
logEvent,
6+
} from "firebase/analytics";
27
import { FirebaseApp, initializeApp } from "firebase/app";
38
import { child, getDatabase, onValue, ref } from "firebase/database";
49

@@ -13,35 +18,35 @@ type UUID = string;
1318

1419
export function addLocationListener(
1520
pullKey: string,
16-
callback: (location: Location) => void
21+
callback: (location: Location) => void,
1722
) {
1823
return forPullKey(pullKey).addLocationListener(callback);
1924
}
2025

2126
export function addSpeedListener(
2227
pullKey: string,
23-
callback: (speed: MetersPerSecond) => void
28+
callback: (speed: MetersPerSecond) => void,
2429
) {
2530
return forPullKey(pullKey).addSpeedListener(callback);
2631
}
2732

2833
export function addHeadingListener(
2934
pullKey: string,
30-
callback: (heading: Degrees) => void
35+
callback: (heading: Degrees) => void,
3136
) {
3237
return forPullKey(pullKey).addHeadingListener(callback);
3338
}
3439

3540
export function addAltitudeListener(
3641
pullKey: string,
37-
callback: (altitude: Meters) => void
42+
callback: (altitude: Meters) => void,
3843
) {
3944
return forPullKey(pullKey).addAltitudeListener(callback);
4045
}
4146

4247
export function addSessionIdListener(
4348
pullKey: string,
44-
callback: (sessionId: UUID | null) => void
49+
callback: (sessionId: UUID | null) => void,
4550
) {
4651
return forPullKey(pullKey).addSessionIdListener(callback);
4752
}
@@ -51,61 +56,71 @@ export function forPullKey(pullKey: string) {
5156
const db = getDatabase(getApp());
5257
const analytics = getAnalytics(getApp());
5358
const reference = child(ref(db, "pullables"), pullKey);
59+
const safeLogEvent = (eventName: string, eventParams?: any) => {
60+
isSupported()
61+
.then((supported) => {
62+
if (supported) {
63+
logEvent(analytics, eventName, eventParams);
64+
}
65+
})
66+
.catch(() => {});
67+
};
68+
5469
return {
5570
addLocationListener(callback: (location: Location) => void) {
56-
logEvent(analytics, "listener", { type: "location", pullKey });
71+
safeLogEvent("listener", { type: "location", pullKey });
5772
return onValue(child(reference, "location"), (snapshot) => {
5873
callback(snapshot.val());
59-
logEvent(analytics, "data", { type: "sessionId", pullKey });
74+
safeLogEvent("data", { type: "sessionId", pullKey });
6075
});
6176
},
6277
addSpeedListener(callback: (speed: MetersPerSecond) => void) {
63-
logEvent(analytics, "listener", { type: "speed", pullKey });
78+
safeLogEvent("listener", { type: "speed", pullKey });
6479
return onValue(child(reference, "speed"), (snapshot) => {
6580
callback(snapshot.val());
66-
logEvent(analytics, "data", { type: "sessionId", pullKey });
81+
safeLogEvent("data", { type: "sessionId", pullKey });
6782
});
6883
},
6984
addHeadingListener(callback: (heading: Degrees) => void) {
70-
logEvent(analytics, "listener", { type: "heading", pullKey });
85+
safeLogEvent("listener", { type: "heading", pullKey });
7186
return onValue(child(reference, "heading"), (snapshot) => {
7287
callback(snapshot.val());
73-
logEvent(analytics, "data", { type: "sessionId", pullKey });
88+
safeLogEvent("data", { type: "sessionId", pullKey });
7489
});
7590
},
7691
addAltitudeListener(callback: (altitude: Meters) => void) {
77-
logEvent(analytics, "listener", { type: "altitude", pullKey });
92+
safeLogEvent("listener", { type: "altitude", pullKey });
7893
return onValue(child(reference, "altitude"), (snapshot) => {
7994
callback(snapshot.val());
80-
logEvent(analytics, "data", { type: "sessionId", pullKey });
95+
safeLogEvent("data", { type: "sessionId", pullKey });
8196
});
8297
},
8398
addHeartRateListener(callback: (altitude: BeatsPerMinute) => void) {
84-
logEvent(analytics, "listener", { type: "heartRate", pullKey });
99+
safeLogEvent("listener", { type: "heartRate", pullKey });
85100
return onValue(child(reference, "heartRate"), (snapshot) => {
86101
callback(snapshot.val());
87-
logEvent(analytics, "data", { type: "sessionId", pullKey });
102+
safeLogEvent("data", { type: "sessionId", pullKey });
88103
});
89104
},
90105
addCyclingPowerListener(callback: (power: Watts) => void) {
91-
logEvent(analytics, "listener", { type: "cyclingPower", pullKey });
106+
safeLogEvent("listener", { type: "cyclingPower", pullKey });
92107
return onValue(child(reference, "cyclingPower"), (snapshot) => {
93108
callback(snapshot.val());
94-
logEvent(analytics, "data", { type: "sessionId", pullKey });
109+
safeLogEvent("data", { type: "sessionId", pullKey });
95110
});
96111
},
97112
addCyclingCrankListener(callback: (power: Watts) => void) {
98-
logEvent(analytics, "listener", { type: "cyclingCrank", pullKey });
113+
safeLogEvent("listener", { type: "cyclingCrank", pullKey });
99114
return onValue(child(reference, "cyclingCrank"), (snapshot) => {
100115
callback(snapshot.val());
101-
logEvent(analytics, "data", { type: "sessionId", pullKey });
116+
safeLogEvent("data", { type: "sessionId", pullKey });
102117
});
103118
},
104119
addCyclingWheelListener(callback: (power: Watts) => void) {
105-
logEvent(analytics, "listener", { type: "cyclingWheel", pullKey });
120+
safeLogEvent("listener", { type: "cyclingWheel", pullKey });
106121
return onValue(child(reference, "cyclingWheel"), (snapshot) => {
107122
callback(snapshot.val());
108-
logEvent(analytics, "data", { type: "sessionId", pullKey });
123+
safeLogEvent("data", { type: "sessionId", pullKey });
109124
});
110125
},
111126
/**
@@ -117,10 +132,10 @@ export function forPullKey(pullKey: string) {
117132
* for a given session id.
118133
*/
119134
addPedometerStepsListener(callback: (steps: Steps) => void) {
120-
logEvent(analytics, "listener", { type: "pedometerSteps", pullKey });
135+
safeLogEvent("listener", { type: "pedometerSteps", pullKey });
121136
return onValue(child(reference, "pedometerSteps"), (snapshot) => {
122137
callback(snapshot.val());
123-
logEvent(analytics, "data", { type: "sessionId", pullKey });
138+
safeLogEvent("data", { type: "sessionId", pullKey });
124139
});
125140
},
126141
/**
@@ -129,10 +144,10 @@ export function forPullKey(pullKey: string) {
129144
* as bluetooth heart rate or telemetry data.
130145
*/
131146
addListener(callback: (data: any) => void) {
132-
logEvent(analytics, "listener", { type: "root", pullKey });
147+
safeLogEvent("listener", { type: "root", pullKey });
133148
return onValue(reference, (snapshot) => {
134149
callback(snapshot.val());
135-
logEvent(analytics, "data", { type: "root", pullKey });
150+
safeLogEvent("data", { type: "root", pullKey });
136151
});
137152
},
138153
/**
@@ -142,10 +157,10 @@ export function forPullKey(pullKey: string) {
142157
* possible for two sequential non-null sessionIds to be sent.
143158
*/
144159
addSessionIdListener(callback: (sessionId: UUID | null) => void) {
145-
logEvent(analytics, "listener", { type: "sessionId", pullKey });
160+
safeLogEvent("listener", { type: "sessionId", pullKey });
146161
return onValue(child(reference, "sessionId"), (snapshot) => {
147162
callback(snapshot.val());
148-
logEvent(analytics, "data", { type: "sessionId", pullKey });
163+
safeLogEvent("data", { type: "sessionId", pullKey });
149164
});
150165
},
151166
};
@@ -154,15 +169,14 @@ export function forPullKey(pullKey: string) {
154169
/** Creates a listener source for a streamer's public data. */
155170
export function forStreamer(provider: "twitch", userId: string) {
156171
const db = getDatabase(getApp());
157-
const analytics = getAnalytics(getApp());
158172
const reference = child(ref(db, "streamers"), `${provider}:${userId}`);
159173
return {
160174
/** If the public location is hidden (eg streamer is offline), null is passed. */
161175
addLocationListener(callback: (location: Location | null) => void) {
162-
logEvent(analytics, "listener", { type: "location", provider, userId });
176+
safeLogEvent("listener", { type: "location", provider, userId });
163177
return onValue(child(reference, "location"), (snapshot) => {
164178
callback(snapshot.val());
165-
logEvent(analytics, "data", { type: "location", provider, userId });
179+
safeLogEvent("data", { type: "location", provider, userId });
166180
});
167181
},
168182
};
@@ -180,7 +194,7 @@ function getApp() {
180194
appId: "1:684852107701:web:d77a8ed0ee5095279a61fc",
181195
measurementId: "G-TR97D81LT3",
182196
},
183-
"rtirl-api"
197+
"rtirl-api",
184198
);
185199
}
186200
return app;

0 commit comments

Comments
 (0)