Skip to content
Home » Bloomreach SPA Tracking: React, Vue & Next.js

Bloomreach SPA Tracking: React, Vue & Next.js

    Bloomreach SPA Tracking: React, Vue & Next.js

    Single-page applications require explicit virtual page view triggers or built-in SDK reloading options because client-side route changes do not trigger standard browser page reloads.

    When a user navigates within a React, Vue, or Next.js application, the browser does not fetch a new HTML document, so Bloomreach does not automatically capture the page transition.

    How mature is your CRM setup?

    Find out in minutes with a free CRM maturity check.

    Get Your Free Check

    The Bloomreach JavaScript SDK provides two mechanisms to address this: the spa_reloading configuration parameters for automatic detection, or manual page_visit event calls dispatched from your router.

    Initializing the SDK at the root level of your application ensures a single tracking instance persists across all route changes. This prevents duplicate SDK instances, session conflicts, and inconsistent event payloads. The SDK initialization happens once when the application loads, before any routing listeners are attached.

    Configuring spa_reloading options in the Bloomreach SDK

    The Bloomreach JavaScript SDK v2.2.0 and later includes spa_reloading configuration parameters that detect URL and hash changes automatically. Setting spa_reloading.on_url_change: true instructs the SDK to listen for changes to the browser’s URL without requiring manual event calls from your router.

    The spa_reloading.visits: true parameter controls whether the SDK fires page_visit events when URL changes are detected. When enabled, the SDK sends a page visit event to Bloomreach each time the URL changes, capturing the new page context.

    The spa_reloading.automatic_tracking: true parameter reloads automatic tracking rules on each URL change, ensuring that rules tied to specific pages re-evaluate after navigation.

    The spa_reloading.banners: true parameter reloads Web Layers (in-app messages and banners) after route changes, allowing you to display different messages on different virtual pages. The spa_reloading.tags: true parameter reloads Tag Manager tags on URL changes. These parameters work independently, so you can enable only the features your implementation requires.

    Enabling spa_reloading.visits: true while simultaneously dispatching manual page_visit calls from your router causes duplicate event logging and distorts session data. Choose either the automatic approach or the manual approach, not both.

    Implementing Bloomreach tracking in React with React Router

    Trigger page_visit events inside React Router location listeners without re-initializing the SDK on component renders by using a custom hook that fires only when the route changes. Create a hook that calls useLocation from React Router and wraps the tracking call in a useEffect with the location pathname as a dependency.

    Inside the useEffect, call window.exponea.track('page_visit', { url: window.location.href }) to dispatch the event to Bloomreach. The URL parameter captures the current page path and query string. The useEffect dependency array ensures the tracking call fires only when the location changes, not on every component render.

    Place this hook in a component at the root level of your application, such as your main layout or router wrapper. This ensures the hook runs once per route transition and persists across all nested routes. Avoid placing tracking hooks in components that unmount and remount during navigation, as this causes duplicate events.

    If you are using the automatic spa_reloading configuration instead of manual tracking, remove the custom hook entirely and rely on the SDK’s built-in detection. Do not combine both approaches.

    Implementing Bloomreach tracking in Vue with Vue Router

    Hook into Vue Router’s afterEach navigation guard to dispatch virtual page views after route changes. The afterEach guard executes after every route transition, making it the ideal place to trigger tracking events without interfering with route loading or component lifecycle.

    Inside the afterEach guard, call window.exponea.track('page_visit', { url: window.location.href }) to send the page visit event to Bloomreach. The guard receives the destination route and the origin route as parameters, giving you access to both the new and previous page context if needed.

    Register the afterEach guard in your router configuration file, before the application mounts. This ensures the guard is active for all route transitions from application startup. If you are using Nuxt with automatic routing, attach the guard in a plugin file that executes on the client side only.

    Wrap the tracking call in a check for the window object to ensure the code runs only in the browser, not during server-side rendering. This prevents errors during build time or static generation.

    Implementing Bloomreach tracking in Next.js App Router

    Set up client components using usePathname and useSearchParams in Next.js App Router to capture route changes and dispatch page visit events. Create a client component that imports these hooks from next/navigation and wraps the tracking logic in a useEffect that depends on the pathname and search parameters.

    The usePathname hook returns the current URL path, and useSearchParams returns an object of query parameters. Combine these to construct the full URL for the page visit event: window.exponea.track('page_visit', { url: window.location.href }).

    For Next.js Pages Router, attach listeners to router.events and trigger tracking on the routeChangeComplete event. Import useRouter from next/router and listen for routeChangeComplete inside a useEffect. This event fires after the route transition completes and the new page is ready, ensuring you capture the correct URL.

    Both App Router and Pages Router require client-side execution. In App Router, use the 'use client' directive at the top of your component file. In Pages Router, ensure tracking logic runs inside a useEffect or event listener, not at module level during server-side rendering.

    Voxwise has found that handling client-side router hooks in server-rendered environments requires careful attention to hydration timing. Initialize the Bloomreach SDK after the client hydrates, not during the server render phase, to avoid session mismatches between the server and browser.

    How mature is your CRM setup?

    Find out in minutes with a free CRM maturity check.

    Get Your Free Check

    Tracking e-commerce product views and purchases in SPAs

    Dispatch view_item, cart_update, and purchase events during dynamic asynchronous client interactions to capture the full e-commerce customer journey. These events fire independently of page navigation and must be triggered by user actions such as clicking a product link, adding to cart, or submitting a purchase form.

    For product views, call window.exponea.track('view_item', { item_id: productId, item_name: productName }) when a user lands on or navigates to a product detail page. Dispatch this event in the same location where you trigger the page visit event to ensure both the page context and the product context are captured.

    For cart updates, call window.exponea.track('cart_update', { item_id: productId, quantity: cartQuantity }) whenever the user adds, removes, or modifies items in the cart. These events fire asynchronously and do not depend on route changes, so attach them to button click handlers or form submission handlers.

    For purchases, call window.exponea.track('purchase', { purchase_id: orderId, total_price: orderTotal }) after the user completes a checkout flow and the order is confirmed. Ensure this event fires after the payment processor confirms the transaction, not before, to avoid logging incomplete or failed purchases.

    SDK ParameterSupported VersionFunctionality
    spa_reloading.on_url_changev2.2.0+Automatically detects URL path changes and triggers configured reload actions
    spa_reloading.on_hash_changev2.2.0+Automatically detects hash fragment changes in the URL
    spa_reloading.visitsv2.2.0+Fires page_visit events automatically when URL or hash changes are detected
    spa_reloading.automatic_trackingv2.2.0+Re-evaluates automatic tracking rules after each URL change
    spa_reloading.bannersv2.2.0+Reloads Web Layers and in-app messages after route transitions

    FAQ

    What parameters belong in page_visit events for virtual page views?

    The page_visit event requires the url parameter to capture the current page path. Include window.location.href to send the full URL including the domain, path, and query string. Additional optional parameters such as title or custom attributes can be added to enrich the page context.

    How do Bloomreach Marketing and Search handle SPA tracking differently?

    Bloomreach Marketing uses the JavaScript SDK and page_visit events or spa_reloading configuration to track page views in SPAs. Bloomreach Search requires explicit trackPageView calls that include the orig_ref_url parameter to log the prior URL for session continuity.

    How do you integrate CMP consent management with Bloomreach SPA tracking?

    Initialize the Bloomreach SDK only after the user grants consent through the Consent Management Platform. Delay the SDK initialization call until the CMP confirms that marketing cookies are permitted. If the user declines, do not initialize the SDK.

    How are Web Layers and experiments re-evaluated on SPA route changes?

    Enable spa_reloading.banners: true to automatically reload Web Layers after each route change. For experiments, call webxpClient.reset() after navigation to re-evaluate experiment rules against the new page context, or dispatch a dataLayer event such as engagement.activate to trigger re-evaluation.

    How do you prevent duplicate page_visit events during SPA navigation?

    Use either the automatic spa_reloading.visits: true configuration or manual page_visit event calls from your router, but not both. If you enable automatic reloading, remove manual tracking calls. If you use manual calls, disable automatic reloading to ensure only one event fires per route change.

    Audit your SPA tracking pipeline

    Development and analytics teams should audit their SPA tracking pipeline to ensure clean event dispatching without duplicate page hits. Validate event output in the Bloomreach Tracking Console to confirm that each route change generates exactly one page visit event and that e-commerce events fire at the correct moments in the customer journey.

    Tags: