UNPKG

google-publisher-tag

Version:

Type-safe React wrapper for Google Publisher Tag (GPT).

30 lines (29 loc) 1.01 kB
"use client"; import { useCallback } from "react"; import { loadGPT } from "./loader"; export function useGlobalTargeting() { const setTargeting = useCallback(async (t) => { const googletag = await loadGPT(); return new Promise((resolve) => { googletag.cmd.push(() => { const pubads = googletag.pubads(); Object.entries(t).forEach(([k, v]) => pubads.setTargeting(k, v)); resolve(); }); }); }, []); return { setTargeting }; } export function useRefresh() { const refresh = useCallback(async (divIds) => { const googletag = await loadGPT(); googletag.cmd.push(() => { const slots = googletag.pubads().getSlots(); const toRefresh = divIds && divIds.length ? slots.filter((s) => divIds.includes(s.getSlotElementId())) : slots; googletag.pubads().refresh(toRefresh); }); }, []); return { refresh }; }