28 lines
644 B
TypeScript
28 lines
644 B
TypeScript
import {useEffect, useRef} from 'react';
|
|
import {useLocation} from 'react-router';
|
|
import {loadMetaPixel} from '~/lib/loadMetaPixel';
|
|
|
|
/**
|
|
* Loads Meta Pixel after hydration and fires `PageView` on SPA navigations.
|
|
*/
|
|
export function MetaPixelPageView() {
|
|
const location = useLocation();
|
|
const isInitialLoad = useRef(true);
|
|
|
|
useEffect(() => {
|
|
loadMetaPixel();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (isInitialLoad.current) {
|
|
isInitialLoad.current = false;
|
|
return;
|
|
}
|
|
if (typeof window.fbq === 'function') {
|
|
window.fbq('track', 'PageView');
|
|
}
|
|
}, [location.pathname, location.search]);
|
|
|
|
return null;
|
|
}
|