Install options
Add Driver Studio with a script tag, or bundle it with the npm package. Both load the same script and give you the same API.
On this page
Script tag
Paste this before </head> on every page of your app. You'll find your site key in the dashboard under Install.
<script async src="https://api.driver.studio/embed.js" data-driver-studio="YOUR_SITE_KEY"></script>That's all. New tours go live from the dashboard without touching your code again. In single-page apps the script follows route changes on its own.
Call it before it loads
The script loads with async, so window.driverStudio is not there right away. Add this small snippet before the script tag. It queues your calls and runs them in order once the script loads.
<script>!function(w){var d=w.driverStudio=w.driverStudio||[];["identify","track","start","on","reset"].forEach(function(m){d[m]=d[m]||function(){d.push([m].concat([].slice.call(arguments)))}})}(window);</script>
<script async src="https://api.driver.studio/embed.js" data-driver-studio="YOUR_SITE_KEY"></script>npm
If you'd rather bundle it, install the package and call init once when your app starts.
npm i @driver-studio/webimport { driverStudio } from '@driver-studio/web'
driverStudio.init({ key: 'YOUR_SITE_KEY' })init adds the script to the page once. Calling it again does nothing. Calls you make before the script has loaded are queued, and every function does nothing on the server, so it is safe in apps that render on the server.
React and Next.js
There is no separate React package. Use the npm package and call init in an effect, so it only runs in the browser.
import { useEffect } from 'react'
import { driverStudio } from '@driver-studio/web'
export function App() {
useEffect(() => {
driverStudio.init({ key: 'YOUR_SITE_KEY' })
}, [])
return <Routes />
}In the Next.js App Router, put it in a small 'use client' component and render it once in your root layout. Call identify in an effect too, whenever your signed-in user changes.
Self-hosting
Running your own Driver Studio server? Point the package at it with apiUrl.
driverStudio.init({ key: 'YOUR_SITE_KEY', apiUrl: 'https://tours.example.com' })