Skip to content

Commit 4fc5344

Browse files
authored
Merge pull request #214 from shayan-deriv/shayan/send-gtm-events
feat: added page_load and allow_tracking events for gtm
2 parents 515b327 + 517c3ca commit 4fc5344

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

global.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ declare global {
33
LC_API: {
44
open_chat_window: VoidFunction;
55
};
6+
dataLayer: {
7+
push: (event: { [key: string]: boolean | number | string; event: string }) => void;
8+
};
69
}
710
}
811

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useLocation } from 'react-router-dom';
2+
import { renderHook } from '@testing-library/react';
3+
import useHandleRouteChange from '../useHandleRouteChange';
4+
5+
jest.mock('react-router-dom', () => ({
6+
useLocation: jest.fn(),
7+
}));
8+
9+
describe('useHandleRouteChange', () => {
10+
beforeEach(() => {
11+
(useLocation as jest.Mock).mockReset();
12+
window.dataLayer = [] as { [key: string]: boolean | number | string; event: string }[];
13+
});
14+
15+
it('should push event to dataLayer on location change', () => {
16+
(useLocation as jest.Mock).mockReturnValue({ pathname: '/initial' });
17+
18+
renderHook(() => useHandleRouteChange());
19+
20+
expect(window.dataLayer).toEqual([{ event: 'page_load' }]);
21+
22+
(useLocation as jest.Mock).mockReturnValue({ pathname: '/new-path' });
23+
24+
renderHook(() => useHandleRouteChange());
25+
26+
expect(window.dataLayer).toEqual([{ event: 'page_load' }, { event: 'page_load' }]);
27+
});
28+
});

src/hooks/custom-hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as useExtendedOrderDetails } from './useExtendedOrderDetails';
44
export { default as useFetchMore } from './useFetchMore';
55
export { default as useFloatingRate } from './useFloatingRate';
66
export { default as useFullScreen } from './useFullScreen';
7+
export { default as useHandleRouteChange } from './useHandleRouteChange';
78
export { default as useIsAdvertiser } from './useIsAdvertiser';
89
export { default as useIsAdvertiserBarred } from './useIsAdvertiserBarred';
910
export { default as useIsP2PBlocked } from './useIsP2PBlocked';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
/**
5+
* Custom hook to handle side effects on route change like pushing events to dataLayer.
6+
*/
7+
const useHandleRouteChange = () => {
8+
const location = useLocation();
9+
useEffect(() => {
10+
window?.dataLayer.push({
11+
event: 'page_load',
12+
});
13+
}, [location]);
14+
};
15+
16+
export default useHandleRouteChange;

src/routes/AppContent/index.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22
import { useHistory, useLocation } from 'react-router-dom';
33
import { BlockedScenarios } from '@/components/BlockedScenarios';
44
import { BUY_SELL_URL, ERROR_CODES } from '@/constants';
@@ -20,6 +20,7 @@ const tabRoutesConfiguration = routes.filter(
2020
);
2121

2222
const AppContent = () => {
23+
const isGtmTracking = useRef(false);
2324
const history = useHistory();
2425
const location = useLocation();
2526
const { isDesktop } = useDevice();
@@ -70,6 +71,13 @@ const AppContent = () => {
7071
setActiveTab(getActiveTab(location.pathname));
7172
}, [location]);
7273

74+
useEffect(() => {
75+
if (!isGtmTracking.current) {
76+
window.dataLayer.push({ event: 'allow_tracking' });
77+
isGtmTracking.current = true;
78+
}
79+
}, []);
80+
7381
const getComponent = () => {
7482
if ((isLoadingActiveAccount || !isFetched || !activeAccountData) && !isEndpointRoute) {
7583
return <Loader />;

src/routes/Router.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { Redirect, Route, RouteComponentProps, Switch } from 'react-router-dom';
33
import { TCurrency } from 'types';
44
import { Page404 } from '@/components';
55
import { BUY_SELL_URL } from '@/constants';
6+
import { useHandleRouteChange } from '@/hooks';
67
import { Loader } from '@deriv-com/ui';
78
import { routes } from './routes-config';
89

910
type TState = { [key: string]: string[] | TCurrency | string; from: string };
10-
1111
declare module 'react-router-dom' {
1212
export function useHistory(): {
1313
goBack: () => void;
@@ -36,6 +36,7 @@ const RouteWithSubRoutes = (route: TRoutesWithSubRoutes) => {
3636
};
3737

3838
const Router: FC = () => {
39+
useHandleRouteChange();
3940
return (
4041
<Suspense fallback={<Loader isFullScreen />}>
4142
<Switch>

0 commit comments

Comments
 (0)