Skip to content

Commit

Permalink
Merge pull request #257 from TogetherCrew/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
cyri113 authored Feb 14, 2024
2 parents 0946bca + 38f3ccd commit 98760a3
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/helpers/amplitudeHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as amplitude from '@amplitude/analytics-browser';
import jwt_decode from 'jwt-decode';
import { StorageService } from '../services/StorageService';
import { IDecodedToken, ITrackEventParams } from '../utils/interfaces';
import {
IDecodedToken,
IDiscordModifiedCommunity,
ITrackEventParams,
} from '../utils/interfaces';
import { IUser } from '../utils/types';
import { useEffect } from 'react';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -44,19 +48,31 @@ export function usePageViewTracking() {
const router = useRouter();

useEffect(() => {
const community =
StorageService.readLocalStorage<IDiscordModifiedCommunity>('community');

function trackPageView(url: string) {
amplitude.logEvent('PAGE_VIEW', { path: url });
const queryParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(queryParams.entries());

amplitude.logEvent('PAGE_VIEW', {
path: url,
communityId: community?.id,
communityName: community?.name,
...params,
});
}

// Track the current page when the component mounts
trackPageView(window.location.pathname);
trackPageView(window.location.pathname + window.location.search);

const handleRouteChange = (url: string) => {
trackPageView(url);
};

// Track subsequent page views on route change
router.events.on('routeChangeComplete', trackPageView);
router.events.on('routeChangeComplete', handleRouteChange);

// Cleanup event listener on unmount
return () => {
router.events.off('routeChangeComplete', trackPageView);
router.events.off('routeChangeComplete', handleRouteChange);
};
}, []);
}

0 comments on commit 98760a3

Please sign in to comment.