• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / fx-private-relay / b28057c3-dfac-4902-bb9f-d3f48629d9da

28 Feb 2024 09:41PM CUT coverage: 74.119% (-0.02%) from 74.139%
b28057c3-dfac-4902-bb9f-d3f48629d9da

push

circleci

web-flow
Merge pull request #4453 from mozilla/fix-early-ga-mpp-3762

MPP-3762: Handle order issue with `useGoogleAnalytics`

2048 of 3013 branches covered (67.97%)

Branch coverage included in aggregate %.

2 of 5 new or added lines in 1 file covered. (40.0%)

6515 of 8540 relevant lines covered (76.29%)

20.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.46
/frontend/src/hooks/googleAnalytics.ts
1
import ReactGa from "react-ga";
21✔
2
import { useState } from "react";
21✔
3
import { singletonHook } from "react-singleton-hook";
21✔
4
import { getRuntimeConfig } from "../config";
21✔
5

6
let gaIsInitialized = false;
21✔
7
let globalEnableGoogleAnalytics: null | ((_: boolean) => void) = null;
21✔
8

9
export const useGoogleAnalytics = singletonHook(gaIsInitialized, () => {
1,334✔
10
  const [isInitialized, setIsInitialized] = useState(gaIsInitialized);
11✔
11
  globalEnableGoogleAnalytics = setIsInitialized;
11✔
12
  return isInitialized;
11✔
13
});
14

15
export function initGoogleAnalytics() {
×
16
  ReactGa.initialize(getRuntimeConfig().googleAnalyticsId, {
×
17
    titleCase: false,
18
    debug: process.env.NEXT_PUBLIC_DEBUG === "true",
19
  });
20
  ReactGa.set({
×
21
    anonymizeIp: true,
22
    transport: "beacon",
23
  });
24
  const cookies = document.cookie.split("; ");
×
25
  const gaEventCookies = cookies.filter((item) =>
×
26
    item.trim().startsWith("server_ga_event:"),
×
27
  );
28
  gaEventCookies.forEach((item) => {
×
29
    const serverEventLabel = item.split("=")[1];
×
30
    if (serverEventLabel) {
×
31
      ReactGa.event({
×
32
        category: "server event",
33
        action: "fired",
34
        label: serverEventLabel,
35
      });
36
    }
37
  });
NEW
38
  if (globalEnableGoogleAnalytics === null) {
×
39
    // useGoogleAnalytics is not complete. Set initial value.
NEW
40
    gaIsInitialized = true;
×
41
  } else {
42
    // Notify listeners that Google Analytics is available.
NEW
43
    globalEnableGoogleAnalytics(true);
×
44
  }
45
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc