# How to track UGC galleries on Hydrogen, Next.js and other headless stores

Mount galleries after each route change. Keep their events flowing to GA4, the pixels and GTM. Then check it in the browser.

By Rohin Aggarwal · 2026-11-26

The gallery shows on the home page. A shopper taps through to a product page, and the gallery space is empty. GA4 shows views from the first page and nothing after. The site is a single-page app, and nothing told the gallery the page changed.

**Quick answer**

- **The loader watches the page for 30 seconds after load.** A gallery that renders later needs a mount call.
- **Call Idukki.mount() from the component that renders the gallery.** Call unmount when it goes away.
- **Can’t touch the components?** Add data-idk-spa="1" to the loader tag. It mounts after each route change.
- **Events need nothing extra.** A mounted gallery sends to GA4, the pixels and GTM as on any store.

This guide gets every product page its gallery, and every gallery its events. It takes one helper function and one component.

## Why the gallery goes missing

The loader runs once, on the first page. It mounts every gallery it finds. Then it watches the page for 30 seconds and mounts any that appear.

A shopper who opens a product page two minutes later gets no gallery. The container is in the page, but nothing is watching any more. No gallery means no events either.

### Two ways to fix it
_Pick the component call when you own the code. Pick the attribute when the snippet goes in through a tag manager._

**Code: Idukki.mount() in a component**
Mount on render, unmount on leave.
- ✓ Exact: runs when the element exists
- ✓ Cleans up timers and listeners on unmount
- ✗ Needs a developer for each framework

**No code: data-idk-spa="1" on the loader**
The widget watches route changes itself.
- ✓ Works from a tag manager
- ✓ Mounts after pushState, replaceState and popstate
- ✗ Does not unmount galleries on routes you leave

## Mount from the component

Add the loader once, in the document head. Then use this helper. It works before and after the loader runs, so load order does not matter.

```js
function idukki(command, target) {
  var api = (window.Idukki = window.Idukki || { q: [] });
  api.q.push([command, target]);
}
```

On Hydrogen or any React store, call it from an effect. The gallery mounts when the element exists and unmounts when it leaves.

```tsx
import { useEffect, useRef } from 'react';

function idukki(command: 'mount' | 'unmount', target: Element) {
  const w = window as any;
  (w.Idukki = w.Idukki || { q: [] }).q.push([command, target]);
}

export function IdukkiGallery({ bguid, guid, productId }: { bguid: string; guid: string; productId?: string }) {
  const ref = useRef<HTMLDivElement>(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    idukki('mount', el);
    return () => idukki('unmount', el);
  }, [guid]);
  return <div ref={ref} data-ugc="idukki" data-bguid={bguid} data-guid={guid} filter-pid={productId} />;
}
```

Mounting twice does nothing, so a re-render is safe. A change of filter-pid refetches on its own, so a variant switch needs no remount.

On Next.js, load the loader with next/script in the root layout. The gallery component is a client component with the same effect.

> **Hydrogen and CSP:** Load the loader with Hydrogen’s Script component so it carries the CSP nonce. The loader copies that nonce onto every tag it adds.

## Or let the widget watch the routes

If the snippet goes in through a tag manager, you may not own the components. Add one attribute to the loader tag instead.

```html
<script src="https://assets.idukki.io/version/script_0" data-idk-spa="1" defer></script>
```

The widget then mounts new galleries after each change of path or query. Hash changes are ignored. It never unmounts, so long sessions should use the component call.

## Getting the events into your tools

Once a gallery mounts, it sends events like a gallery on any theme. Your tags only need to be on the page.

| Tool | What to check |
| --- | --- |
| GA4 | The Google tag loads in your root layout, once. |
| Meta, TikTok, Pinterest | Each pixel loads once, or our team adds its ID in Idukki. |
| Google Tag Manager | The container loads once. Gallery moments arrive as idukki_ugc. |
| Matomo or VWO | Route idukki_ugc through GTM, as for any store. |

_What a headless store needs for each tool._

Your own page views on route changes are a separate job. In GTM, a History Change trigger fires on pushState. The gallery’s events do not depend on it.

The event list is in the [widget event reference](/help/widget-event-reference). The GTM setup is in [one GTM trigger for every widget moment](/help/gtm-route-idukki-events).

## Check it in five minutes

**A quick test on a preview build**
1. **Load the home page** — The gallery shows. GA4 Realtime gets idukki_ugc_widget_view. (0 to 1 min)
2. **Go to a product page** — Wait past 30 seconds first. The gallery should still mount. (1 to 2 min)
3. **Tap a tile** — GA4 gets idukki_gallery_click. GTM Preview shows idukki_ugc with post_click. (2 to 4 min)
4. **Go back** — The home page gallery mounts again. Mounting twice does nothing. (4 to 5 min)

### FAQs

**Q: Do I need to unmount galleries?**

A: Yes, when a route that held one is destroyed. Unmount stops its timers and listeners.

**Q: Will SSR break the gallery?**

A: No. Call mount only in the browser, from an effect. The container renders on the server as plain markup.

**Q: Can I render tiles on the server?**

A: Yes. Read the gallery’s public JSON payload. See /platforms/custom-headless for the snippet and the payload.

**Your next step:** Add the helper and the component to the template that renders product pages. Then run the five-minute check on a preview build.

### Sources
- [Tag Manager Help, History change trigger](https://support.google.com/tagmanager/answer/7679322) — Fires on URL fragment changes and the pushState API. Read 26 November 2026.
- [Idukki help, Embed on Hydrogen, Next.js, Astro, Remix](/help/embed-on-headless) — Install snippets for each framework.

---
Canonical: https://idukki.io/blog/track-ugc-galleries-headless-hydrogen-nextjs
Tags: headless, hydrogen, nextjs, ga4, google-tag-manager, single-page-app
