UXWizz
WebsitePricingDemoTwitter (X)
  • Introduction
  • 🛠️ Installation
    • Requirements
      • Limitations
      • Server specs (CPU, RAM)
    • Installation guide
      • Uploading the script
      • Creating a MySQL database
      • Running the installer
    • Install on a new server
      • Ubuntu 20.04 (or higher)
      • DigitalOcean
    • Docker
      • Via Docker Compose
      • Standalone Docker image
    • Adding the tracking code
      • Automatic SPA pageview tracking
      • Add tracker to Next.js App
      • Add tracker to React Router App
    • Optimization tips
      • MySQL/MariaDB
      • Auto-delete old data (cron jobs)
      • Apache
    • Frequently Asked Questions
  • 🔧JavaScript API
    • Tags
    • Events
    • Hooks/callbacks
    • Other API functions
    • Session Recording
      • Ignore specific elements
  • 📖Guides and features
    • Goals (NEW!)
    • Ask AI (NEW!)
    • Basic usage
    • A/B testing
    • Usage tips
    • Dashboard user access level
    • Resetting the admin password
    • Database querying
    • Technical details
      • IP Geolocation
    • Troubleshooting
      • Agency
        • MultiDB
      • Dashboard
        • Refreshing dashboard sub-page leads to 404 error
        • Website iframe not loading (x-frame-options)
        • License says "invalid"
        • Updating Fails
      • Tracking
        • No data is being recorded
        • Can't include tracker via Google Tag Manager
        • The A/B test JS file is missing
      • WordPress
        • Cloudways 403 Forbidden screen on WordPress
        • NGINX 403 Forbidden screen on WordPress
    • Extending the dashboard
    • Support
    • Migrating to a new server
  • 🎓Useful Examples
    • Feedback form (polls)
    • Tracking 404 Pages
    • Tracking UTM parameters
    • Tracking Google Ads GCLID
    • Storing user device types
    • Track video playback
  • 📜About
    • Changelog
    • Personal Data Information
    • Privacy Policy (uxwizz.com)
    • Licenses and pricing
    • [Deprecated] License Subscriptions
Powered by GitBook
On this page
  • 1. Include the UXWizz tracking snippet in your index.html file
  • 2. Track pageviews

Was this helpful?

  1. 🛠️ Installation
  2. Adding the tracking code

Add tracker to React Router App

1. Include the UXWizz tracking snippet in your index.html file

You should now be able to use UST in your application. If you use TypeScript, you can declare UST as any. TypeScript typings for the tracker will be released soon.

2. Track pageviews

By default, being a SPA, only the entry pageview will be tracked.

To track all consequent pageviews, include a new UXWizzPageview.ts in your app

import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

export default function UXWizzPageview() {
    const location = useLocation();
    const isInitialLoad = useRef(true);

    useEffect(() => {
        if (isInitialLoad.current) {
            isInitialLoad.current = false
            return;
        };
        if (typeof window === 'undefined') return;
        const ust = (window as typeof window & { UST: { trackNewPage: () => void } })['UST'];
        if (!ust || !ust.trackNewPage) return;
        ust.trackNewPage(); // Call on route change, but not on initial load
    }, [location]);
    
    return null;
};

Then, in your main App component, include UXWizzPageview.

// ...
import UXWizzPageview from './UXWizzPageview';

export default function App() {
  return (
    <Router>
      <UXWizzPageview/>
      {/* your other components */}
    </Router>
  );
}

UXWizz will automatically track the initial page-load (when the tracker file is loaded).

We added a check, to avoid tracking the initial page-load again.

If you don't use TypeScript, you can simply remove the typings replace this line:

const ust = (window as typeof window & { UST: { trackNewPage: () => void } })['UST'];

with

const ust = window['UST'];
PreviousAdd tracker to Next.js AppNextOptimization tips

Last updated 7 months ago

Was this helpful?