Skip to content

Commit

Permalink
Merge pull request #1023 from near/log_clientside_referrer
Browse files Browse the repository at this point in the history
 listen to route change events to track the referring url for client side routing
  • Loading branch information
charleslavon authored Mar 5, 2024
2 parents a4d2306 + ccd47e7 commit 25d586c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useBosComponents } from '@/hooks/useBosComponents';
import { useHashUrlBackwardsCompatibility } from '@/hooks/useHashUrlBackwardsCompatibility';
import { usePageAnalytics } from '@/hooks/usePageAnalytics';
import { useAuthStore } from '@/stores/auth';
import { init as initializeAnalytics } from '@/utils/analytics';
import { init as initializeAnalytics, setReferrer } from '@/utils/analytics';
import { setNotificationsLocalStorage } from '@/utils/notificationsLocalStorage';
import type { NextPageWithLayout } from '@/utils/types';
import { styleZendesk } from '@/utils/zendesk';
Expand Down Expand Up @@ -54,6 +54,13 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {
}
}, [signedIn]);

useEffect(() => {
router.events.on('routeChangeStart', () => {
//save a reference to the currentl URL before the route change event completes
setReferrer(window.location.href);
});
});

useEffect(() => {
initializeAnalytics();
}, []);
Expand Down
9 changes: 9 additions & 0 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ let hashId = '';
let anonymousUserIdCreatedAt = '';
let pendingEvents: any = [];
let cookieOptOut = false;
let clientsideReferrer = '';

declare global {
interface Window {
rudderAnalytics: Analytics | undefined;
}
}

export function setReferrer(referrer: string) {
clientsideReferrer = referrer;
}

export function setAccountIdHash(accountId: string) {
const hash = createHash('sha512');
hash.update(accountId);
Expand Down Expand Up @@ -115,6 +120,7 @@ export function recordPageView(pageName: string) {
url: filterURL(window.location.href),
userAgentDetail,
ref: filterURL(document.referrer),
clientsideReferrer,
});
} catch (e) {
console.error(e);
Expand All @@ -128,6 +134,7 @@ const record = (eventType: string, e: UIEvent | PointerEvent) => {
url: e.target ? filterURL((e.target as HTMLElement).baseURI) : '',
xPath: getXPath(e.target as HTMLElement),
componentSrc: getComponentName(e.target as HTMLElement),
clientsideReferrer,
});
};
export const recordClick = (e: UIEvent | PointerEvent) => record('click', e);
Expand Down Expand Up @@ -163,6 +170,7 @@ export function recordEventWithProps(eventLabel: string, properties: Record<stri
userAgentDetail,
hashId: localStorage.getItem('hashId'),
anonymousUserIdCreatedAt,
clientsideReferrer,
});
} catch (e) {
console.error(e);
Expand All @@ -181,6 +189,7 @@ export function recordEvent(eventLabel: string) {
url: window.location.href,
userAgentDetail,
anonymousUserIdCreatedAt,
clientsideReferrer,
});
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 25d586c

Please sign in to comment.