* add gtm to load in us only * fix: Tried to give typescript some help * fix: Alternative fix * fix: Add yarn.lock * fix: Oops * fix: type err --------- Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: Udit Takkar <udit222001@gmail.com>
55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
import { GoogleTagManager } from "@next/third-parties/google";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID;
|
|
|
|
const CACHE_KEY = "user_geolocation";
|
|
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
|
|
|
|
export async function fetchGeolocation() {
|
|
const cachedData = localStorage.getItem(CACHE_KEY);
|
|
|
|
if (cachedData) {
|
|
const { country, timestamp } = JSON.parse(cachedData);
|
|
|
|
if (Date.now() - timestamp < CACHE_DURATION) {
|
|
return { country };
|
|
}
|
|
}
|
|
|
|
const res = await fetch("/api/geolocation");
|
|
const data = await res.json();
|
|
|
|
const newCacheData = {
|
|
country: data.country,
|
|
timestamp: Date.now(),
|
|
};
|
|
|
|
localStorage.setItem(CACHE_KEY, JSON.stringify(newCacheData));
|
|
return data;
|
|
}
|
|
|
|
export function useGeolocation() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["geolocation"],
|
|
queryFn: fetchGeolocation,
|
|
staleTime: 24 * 60 * 60 * 1000, // 24 hours
|
|
});
|
|
|
|
return {
|
|
isUS: data?.country === "US",
|
|
loading: isLoading,
|
|
error,
|
|
};
|
|
}
|
|
|
|
export function GoogleTagManagerComponent() {
|
|
const { isUS, loading } = useGeolocation();
|
|
|
|
if (!isUS || !GTM_ID || loading) {
|
|
return null;
|
|
}
|
|
|
|
return <GoogleTagManager gtmId={GTM_ID} />;
|
|
}
|