-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
page.ts
52 lines (44 loc) · 1.38 KB
/
page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { getAnalytics, logEvent } from "firebase/analytics";
import * as React from "react";
import { useLocation } from "react-router-dom";
const appName = import.meta.env.VITE_APP_NAME;
export function usePageEffect(
options?: Options,
deps: React.DependencyList = [],
) {
const location = useLocation();
// Once the page component was rendered, update the HTML document's title
React.useEffect(() => {
const previousTitle = document.title;
document.title =
location.pathname === "/"
? options?.title ?? appName
: options?.title
? `${options.title} - ${appName}`
: appName;
return function () {
document.title = previousTitle;
};
}, [
...deps /* eslint-disable-line react-hooks/exhaustive-deps */,
location,
options?.title,
]);
// Send "page view" event to Google Analytics
// https://support.google.com/analytics/answer/11403294?hl=en
React.useEffect(() => {
if (!(options?.trackPageView === false)) {
logEvent(getAnalytics(), "page_view", {
page_title: options?.title ?? appName,
page_path: `${location.pathname}${location.search}`,
});
}
}, [location, options?.title, options?.trackPageView]);
}
type Options = {
title?: string;
/** @default true */
trackPageView?: boolean;
};