-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
Script
component, load plausible analytics from useConfig inst…
…ead of env, redact IDs in URLs (#5229) * fixes #5228 * Adds a `Script` component * include Plausible script in App * remove script from index.html * redact numeric sequences and uuids from plausible analytics (#5240) * this should work now :) * updated script component that fixes issue in firefox * remove empty query params and redacts if non-empty * fix special char issues and redact uuids before numeric ids * fix empty qparams not being removed and blacklist some qparams
- Loading branch information
1 parent
e47b65f
commit ed40094
Showing
7 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useLocationChange } from "raviger"; | ||
import useConfig from "../../Common/hooks/useConfig"; | ||
import Script from "./Script"; | ||
import { useEffect } from "react"; | ||
|
||
export default function Plausible() { | ||
const { site_url, analytics_server_url } = useConfig(); | ||
|
||
useLocationChange(() => triggerPageView()); | ||
useEffect(() => triggerPageView(), []); | ||
|
||
return ( | ||
<Script | ||
defer | ||
data-domain={site_url} | ||
src={`${analytics_server_url}/js/script.manual.js`} | ||
/> | ||
); | ||
} | ||
|
||
const BLACKLISTED_QUERY_PARAMS = ["page", "limit"]; | ||
|
||
const triggerPageView = () => { | ||
const plausible = (window as any).plausible; | ||
|
||
const url = new URL(window.location.href); | ||
|
||
// Remove all blacklisted and empty query parameters | ||
[...url.searchParams.entries()].map(([key, value]) => { | ||
if (value === "" || BLACKLISTED_QUERY_PARAMS.includes(key)) { | ||
url.searchParams.delete(key); | ||
} | ||
}); | ||
|
||
const redactedUrl = url | ||
.toString() | ||
// Replace all uuids in the URL with "ID_REDACTED" | ||
.replace( | ||
/[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}/gi, | ||
"ID_REDACTED" | ||
) | ||
// Replace all numbers in the URL's path params with "ID_REDACTED" | ||
.replace(/\/\d+/g, "/ID_REDACTED"); | ||
|
||
// Send the pageview event to Plausible | ||
plausible("pageview", { u: redactedUrl }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect } from "react"; | ||
|
||
interface Props { | ||
id?: string; | ||
defer?: boolean; | ||
src: string; | ||
[key: string]: any; | ||
} | ||
|
||
/** | ||
* Dynamically load a script into the page. | ||
* To refresh the script, change the `key` prop. | ||
*/ | ||
export default function Script({ id, defer = false, src, ...attrs }: Props) { | ||
useEffect(() => { | ||
const script = document.createElement("script"); | ||
|
||
if (id) script.id = id; | ||
script.src = src; | ||
script.async = true; | ||
script.defer = defer; | ||
|
||
Object.entries(attrs).forEach((e) => script.setAttribute(...e)); | ||
|
||
document.body.appendChild(script); | ||
|
||
return () => { | ||
document.body.removeChild(script); | ||
}; | ||
}, [id, defer, src]); | ||
|
||
return null; | ||
} |