Blue Tracely Install Guide for React.js

Add the Blue Tracely snippet to a React app's HTML template, or load it once from your top-level component, and what to expect when pages change in the browser.

This guide shows two ways to add the Blue Tracely snippet to a React application. Using Next.js? See Blue Tracely Install Guide for Next.js.

Before you begin

The Add the script to your HTML page, with the snippet and its Copy button

How the script behaves in a React app

The script reports one page view per full page load. When your app changes pages in the browser, for example with a client-side router, no new page loads, so those later pages aren't reported. The visitor can still be identified from the page they loaded first.

Loading the snippet again on each route change doesn't add page views: the snippet does nothing when the script is already on the page. See Single Page vs. Multi-Page Sites: A Data Collection Perspective.

Most React apps are served from one HTML file, for example index.html in a Vite project or public/index.html in a Create React App project.

  1. Open that file.
  2. Paste your snippet, unchanged, just before the closing </head> tag.
  3. Build and deploy your app.
<head>
	<meta charset="UTF-8" />
	<title>Your app</title>
	<!-- Your Blue Tracely snippet goes here, just before </head> -->
</head>

This places the snippet in the page head, where it loads earliest.

Option 2: Your top-level component

If you can't edit the HTML template, add the snippet's code from your top-level component, such as App.jsx, so it runs once when the app starts. Use only the code between <script> and </script> from your snippet, unchanged:

import { useEffect } from "react";

const BLUE_TRACELY_SNIPPET = `/* Paste the code between <script> and </script> from your snippet here */`;

export default function App() {
	useEffect(() => {
		if (document.getElementById("blue-tracely")) return;
		const script = document.createElement("script");
		script.id = "blue-tracely";
		script.text = BLUE_TRACELY_SNIPPET;
		document.head.appendChild(script);
	}, []);

	return <div className="App">{/* Your components */}</div>;
}

The snippet's code contains no backticks, so it fits inside the template literal as it is. This option runs a little later than Option 1, because it waits for React to start.

Deploy and check

Deploy your app, open your live site, then go back to the Script page and select Test script next to your domain. When the script answers, you'll see "The script is installed on your website." See Verifying Your Script Installation.

Don't add the script file with its own <script src="..."> tag in place of the snippet. The snippet sets things up before the file loads, and without it Test script reports that your website is running a different version of the script.