'use client'; import { useEffect } from 'react'; import { SALES_CHANNEL_ID } from '@/core/lib/brainerce'; /** * Google Customer Reviews — Google's own post-purchase SURVEY opt-in module * feeding the seller rating shown next to a merchant's ads/search listing. * NOT Maps/Business reviews, and NOT Brainerce's own storefront product- * review feature. * * Fetches this store's survey data directly rather than through the * `brainerce` SDK's `getGoogleCustomerReviewsSurvey` method (SDK 3.3.0 and * later). This mirrors the SDK's own `getOrderByCheckout` request shape exactly: the same * possession-of-`checkoutId` trust model, routed through the same same- * origin BFF proxy (`/api/store`) the rest of this storefront already uses. * * Renders no visible markup. The static `` marker the Readiness tab's * embed-detection check looks for lives in `../app/order-confirmation/ * layout.tsx` instead — that one has to be server-rendered to be visible to * a plain HTTP fetch; nothing this client component does after mount can be. */ interface GoogleCustomerReviewsSurveyData { enabled: boolean; merchantId?: string; orderId?: string; email?: string; deliveryCountry?: string; estimatedDeliveryDate?: string; lang?: string; languageSupported?: boolean; } declare global { interface Window { gapi?: { load: (name: string, cb: () => void) => void; surveyoptin?: { render: (opts: Record) => void } }; renderOptIn?: () => void; ___gcfg?: { lang?: string }; } } // Module-level, not component state: survives this component unmounting and // remounting on the SAME checkout id (React Strict Mode's double-invoke, // or navigating away and back within one browser session) without opening // Google's dialog twice. A full page refresh naturally resets this, which is // correct — the whole point is a fresh render for a fresh page load. let renderedForCheckoutId: string | null = null; export function GoogleCustomerReviewsOptIn({ checkoutId }: { checkoutId: string }) { useEffect(() => { if (!checkoutId || renderedForCheckoutId === checkoutId) return; let cancelled = false; async function run() { try { const res = await fetch( `/api/store/api/vc/${encodeURIComponent(SALES_CHANNEL_ID)}/checkout/${encodeURIComponent(checkoutId)}/google-customer-reviews` ); if (!res.ok || cancelled) return; const survey = (await res.json()) as GoogleCustomerReviewsSurveyData; if (!survey?.enabled || cancelled) return; if ( !survey.merchantId || !survey.orderId || !survey.email || !survey.deliveryCountry || !survey.estimatedDeliveryDate ) { // A Google-side or Brainerce-side gap must never break order // confirmation — skip silently rather than call render() with // missing required fields. return; } renderedForCheckoutId = checkoutId; // Optional language hint MUST be set before platform.js loads. if (survey.languageSupported && survey.lang) { window.___gcfg = { lang: survey.lang }; } window.renderOptIn = function renderOptIn() { window.gapi?.load('surveyoptin', function surveyOptinReady() { window.gapi?.surveyoptin?.render({ merchant_id: survey.merchantId!, order_id: survey.orderId!, email: survey.email!, delivery_country: survey.deliveryCountry!, estimated_delivery_date: survey.estimatedDeliveryDate!, }); }); }; // Google's own required embed: a script tag loading platform.js with // onload=renderOptIn. Explicitly NOT delivered through Google Tag // Manager — Google does not support that for this integration. // Created via the DOM API (no nonce) rather than `next/script`, // mirroring how the SDK's own `loadGoogleAnalytics` loads gtag.js: // under this storefront's `script-src 'strict-dynamic'` CSP, a // script inserted by an already-trusted script (this bundle) is // trusted too, regardless of its own attributes. const script = document.createElement('script'); script.src = 'https://apis.google.com/js/platform.js?onload=renderOptIn'; script.async = true; script.defer = true; document.body.appendChild(script); } catch (err) { // Never break order confirmation over this. console.warn('Google Customer Reviews survey not loaded:', err); } } run(); return () => { cancelled = true; }; }, [checkoutId]); return null; }