-
Notifications
You must be signed in to change notification settings - Fork 4
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 #85 from middlewarehq/feat/mixpanel-client-integra…
…tion feat: analytics event setup
- Loading branch information
Showing
8 changed files
with
188 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ALLOW_TRACKING_KEY } from '@/constants/events'; | ||
import { useCallback } from 'react'; | ||
import toast from 'react-hot-toast'; | ||
import { useLocalStorage } from 'usehooks-ts'; | ||
|
||
export const useTrackingConsent = () => { | ||
const [trackingAllowed, setTrackingAllowed] = useLocalStorage<Boolean | null>( | ||
ALLOW_TRACKING_KEY, | ||
null | ||
); | ||
|
||
const updatedTrackingState = useCallback( | ||
(allowed: boolean) => { | ||
toast.dismiss(ALLOW_TRACKING_KEY); | ||
setTrackingAllowed(allowed); | ||
}, | ||
[setTrackingAllowed] | ||
); | ||
|
||
return useCallback(() => { | ||
if (trackingAllowed || trackingAllowed !== null) return; | ||
return toast.custom( | ||
(t) => { | ||
return ( | ||
<div | ||
className={`${ | ||
t.visible ? 'animate-enter' : 'animate-leave' | ||
} max-w-3xl w-full bg-white shadow-lg rounded-lg pointer-events-auto flex flex-col md:flex-row ring-1 ring-black ring-opacity-5 `} | ||
style={{ backgroundColor: '#16161a' }} | ||
> | ||
<div className="flex-1 p-4"> | ||
This website uses analytics to ensure you get the best experience. | ||
</div> | ||
<div className="flex gap-5 items-center cursor-pointer p-2 justify-around"> | ||
<button | ||
className="text-indigo-600 text-sm focus:outline-none hover:underline" | ||
style={{ color: '#7f5af0' }} | ||
onClick={() => updatedTrackingState(false)} | ||
> | ||
Decline | ||
</button> | ||
<button | ||
className="bg-indigo-500 px-2 py-1 text-white rounded-md text-sm hover:bg-indigo-700 focus:outline-none" | ||
style={{ backgroundColor: '#7f5af0' }} | ||
onClick={() => updatedTrackingState(true)} | ||
> | ||
Allow Analytics | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}, | ||
{ | ||
duration: 20000, | ||
id: ALLOW_TRACKING_KEY, | ||
position: 'bottom-center' | ||
} | ||
); | ||
}, [trackingAllowed, updatedTrackingState]); | ||
}; |
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,29 @@ | ||
import mixpanel from 'mixpanel-browser'; | ||
|
||
import { flattenObj } from '@/utils/datatype'; | ||
import { objectEnum } from '@/utils/enum'; | ||
|
||
enum TrackEventEnum { | ||
WINDOW_FOCUS, | ||
WINDOW_BLUR, | ||
WINDOW_UNLOAD, | ||
CREATE_UNWRAPPED_IMAGES_CLICKED, | ||
UNWRAP_YOUR_YEAR_CLICKED, | ||
SIGN_OUT_CLICKED, | ||
ZIP_DOWNLOAD_CLICKED, | ||
PDF_DOWNLOAD_CLICKED, | ||
LINKEDIN_SHARE_CLICKED, | ||
SINGLE_IMAGE_SHARE_CLICKED, | ||
NEXT_IMAGE_CLICKED, | ||
PREV_IMAGE_CLICKED | ||
} | ||
|
||
export const ALLOW_TRACKING_KEY = 'ALLOW_TRACKING'; | ||
|
||
export const TrackEvents = objectEnum(TrackEventEnum); | ||
|
||
export const track = (ev: keyof typeof TrackEvents, props: any = {}) => { | ||
const allowTracking = localStorage.getItem(ALLOW_TRACKING_KEY) === 'true'; | ||
if (!allowTracking) return; | ||
mixpanel.track(ev, flattenObj(props)); | ||
}; |
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