-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from translate-tools/478-prepare-landing-for-…
…public Prepare landing for public
- Loading branch information
Showing
10 changed files
with
330 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,57 @@ | ||
import React, { PropsWithChildren, useCallback, useEffect, useState } from 'react'; | ||
import Plausible from 'plausible-tracker'; | ||
import { PlausibleInitOptions } from 'plausible-tracker/build/main/lib/tracker'; | ||
|
||
import { analyticsContext, IAnalyticsContext } from './useAnalyticsContext'; | ||
|
||
export const AnalyticsProvider = ({ | ||
options, | ||
children, | ||
}: PropsWithChildren<{ options: PlausibleInitOptions }>) => { | ||
const [plausible] = useState(() => Plausible(options)); | ||
|
||
const trackEvent: IAnalyticsContext['trackEvent'] = useCallback( | ||
(eventName, props) => { | ||
const timestamp = performance.now(); | ||
plausible.trackEvent(eventName, { | ||
// Additional props for every event | ||
props: { | ||
// Current location | ||
location: location.toString(), | ||
// Time since visit page | ||
timestamp: timestamp, | ||
timestampSeconds: timestamp / 1000, | ||
...props, | ||
}, | ||
}); | ||
}, | ||
[plausible], | ||
); | ||
|
||
// Setup default analytic listeners | ||
useEffect(() => { | ||
plausible.enableAutoPageviews(); | ||
plausible.enableAutoOutboundTracking(); | ||
|
||
// Track clicks | ||
document.body.addEventListener('click', (event: MouseEvent) => { | ||
// Explore click targets to find a link element | ||
const targets = event?.composedPath() || [event.target]; | ||
for (const target of targets) { | ||
if (!(target instanceof HTMLAnchorElement)) continue; | ||
|
||
trackEvent('Link click', { | ||
url: target.href, | ||
text: target.innerText, | ||
}); | ||
break; | ||
} | ||
}); | ||
}, [plausible, trackEvent]); | ||
|
||
return ( | ||
<analyticsContext.Provider value={{ trackEvent }}> | ||
{children} | ||
</analyticsContext.Provider> | ||
); | ||
}; |
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,17 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
export type IAnalyticsContext = { | ||
trackEvent: (eventName: string, props: Record<string, string>) => void; | ||
}; | ||
|
||
export const analyticsContext = createContext<IAnalyticsContext>(null as any); | ||
|
||
export const useAnalyticsContext = () => { | ||
const context = useContext(analyticsContext); | ||
if (context === null) | ||
throw new Error( | ||
'Analytics context is not available. Make sure component is wrapped with AnalyticsProvider', | ||
); | ||
|
||
return context; | ||
}; |
Oops, something went wrong.