Overview
Installing Pirsch analytics is straightforward and can be accomplished in minutes. Pirsch offers multiple installation methods to accommodate different website architectures and technical requirements, from simple HTML script tags to npm packages for modern JavaScript frameworks.
This guide walks you through each installation method, helping you choose the right approach for your website and get Pirsch running quickly while maintaining user privacy and GDPR compliance without requiring cookie consent banners.
Prerequisites
Before installing Pirsch, ensure you have:
- Pirsch Account: Sign up at pirsch.io
- Website Domain: Add your domain in the Pirsch dashboard
- Identification Code: Get your unique code from Settings > Websites
- Website Access: Ability to add code to your website's HTML
Installation Methods
Method 1: Basic Script Tag (Recommended for Most Sites)
The simplest installation method for standard websites. This tracks pageviews automatically but doesn't include custom event tracking.
Step 1: Get Your Identification Code
- Log into your Pirsch dashboard
- Navigate to Settings > Websites
- Copy your identification code
Step 2: Add Script to Your Website
Add this script tag in the <head> or before the closing </body> tag:
<script defer src="https://api.pirsch.io/pirsch.js"
id="pirschjs"
data-code="YOUR_IDENTIFICATION_CODE"></script>
Example in HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
<!-- Pirsch Analytics -->
<script defer src="https://api.pirsch.io/pirsch.js"
id="pirschjs"
data-code="YOUR_IDENTIFICATION_CODE"></script>
</head>
<body>
<!-- Your website content -->
</body>
</html>
Why Use defer?
The defer attribute ensures the script:
- Downloads in parallel with HTML parsing
- Executes after the DOM is ready
- Doesn't block page rendering
- Improves page load performance
Method 2: Extended Script (With Custom Events)
Use this version if you need to track custom events, button clicks, form submissions, or other user interactions.
<script defer src="https://api.pirsch.io/pirsch-extended.js"
id="pirschextendedjs"
data-code="YOUR_IDENTIFICATION_CODE"></script>
Full example with event tracking:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Website</title>
<!-- Pirsch Extended Analytics -->
<script defer src="https://api.pirsch.io/pirsch-extended.js"
id="pirschextendedjs"
data-code="YOUR_IDENTIFICATION_CODE"></script>
</head>
<body>
<button id="cta-button">Subscribe Now</button>
<script>
// Track button click
document.getElementById('cta-button').addEventListener('click', function() {
pirsch('CTA Click', {
meta: {
button: 'Subscribe',
location: 'Hero'
}
});
});
</script>
</body>
</html>
Method 3: Script Configuration Options
Customize Pirsch behavior with data attributes:
<script defer src="https://api.pirsch.io/pirsch-extended.js"
id="pirschextendedjs"
data-code="YOUR_CODE"
data-disable-cookies="true"
data-dev="localhost,dev.example.com,*.test"
data-disable-scripts="true"
data-disable-outbound-links="true"></script>
Configuration Options:
| Attribute | Description | Default |
|---|---|---|
data-code |
Your identification code (required) | - |
data-disable-cookies |
Disable all cookies | false |
data-dev |
Comma-separated domains to ignore | - |
data-disable-scripts |
Disable automatic script tracking | false |
data-disable-outbound-links |
Disable outbound link tracking | false |
Method 4: NPM Package for JavaScript Frameworks
For modern JavaScript frameworks like React, Vue, or Next.js, you can use npm packages.
Installation
npm install pirsch-sdk
React Implementation
import { useEffect } from 'react';
import { Pirsch } from 'pirsch-sdk';
function App() {
useEffect(() => {
const pirsch = new Pirsch({
code: 'YOUR_IDENTIFICATION_CODE',
dev: process.env.NODE_ENV === 'development'
});
// Track pageview
pirsch.hit();
// Track custom event
pirsch.event('App Loaded', {
framework: 'React',
version: '18.0'
});
}, []);
return <div>My App</div>;
}
Vue.js Implementation
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import { Pirsch } from 'pirsch-sdk';
const app = createApp(App);
// Initialize Pirsch
const pirsch = new Pirsch({
code: 'YOUR_IDENTIFICATION_CODE'
});
// Make available globally
app.config.globalProperties.$pirsch = pirsch;
app.mount('#app');
Next.js Implementation
// pages/_app.js
import { useEffect } from 'react';
import { useRouter } from 'next/router';
function MyApp({ Component, pageProps }) {
const router = useRouter();
useEffect(() => {
// Load Pirsch script
const script = document.createElement('script');
script.src = 'https://api.pirsch.io/pirsch.js';
script.defer = true;
script.setAttribute('id', 'pirschjs');
script.setAttribute('data-code', process.env.NEXT_PUBLIC_PIRSCH_CODE);
document.head.appendChild(script);
// Track route changes
const handleRouteChange = () => {
if (window.pirsch) {
window.pirsch.hit();
}
};
router.events.on('routeChangeComplete', handleRouteChange);
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);
return <Component {...pageProps} />;
}
export default MyApp;
Method 5: Server-Side Tracking
For applications that need ad-blocker immunity or server-rendered tracking.
Get Access Token
- Go to Pirsch dashboard
- Navigate to Settings > API
- Generate a new access token
- Store securely in environment variables
Node.js/Express Example
const axios = require('axios');
async function trackPageview(req) {
try {
await axios.post('https://api.pirsch.io/api/v1/hit', {
url: `https://example.com${req.path}`,
ip: req.ip,
user_agent: req.headers['user-agent'],
accept_language: req.headers['accept-language'],
referrer: req.headers['referer'] || ''
}, {
headers: {
'Authorization': `Bearer ${process.env.PIRSCH_ACCESS_TOKEN}`,
'Content-Type': 'application/json'
}
});
} catch (error) {
console.error('Pirsch tracking failed:', error.message);
}
}
// Middleware
app.use((req, res, next) => {
trackPageview(req);
next();
});
Platform-Specific Installation
WordPress
Option 1: Code Insertion Plugin
- Install "Insert Headers and Footers" plugin
- Go to Settings > Insert Headers and Footers
- Paste Pirsch script in the header section
Option 2: Theme Functions
Add to your theme's functions.php:
function add_pirsch_analytics() {
?>
<script defer src="https://api.pirsch.io/pirsch.js"
id="pirschjs"
data-code="YOUR_CODE"></script>
<?php
}
add_action('wp_head', 'add_pirsch_analytics');
Shopify
- Go to your Shopify admin
- Navigate to Online Store > Themes
- Click Actions > Edit code
- Open
theme.liquid - Add Pirsch script before
</head>
<!-- Pirsch Analytics -->
<script defer src="https://api.pirsch.io/pirsch.js"
id="pirschjs"
data-code="YOUR_CODE"></script>
Webflow
- Go to Project Settings
- Click Custom Code tab
- Paste Pirsch script in "Head Code" section
- Publish your site
Squarespace
- Go to Settings > Advanced > Code Injection
- Paste Pirsch script in Header section
- Save changes
Ghost
- Go to Settings > Code Injection
- Paste Pirsch script in Site Header section
- Save changes
Validation and Testing
Step 1: Verify Script Installation
Check Script Load:
- Open your website
- Press F12 to open Developer Tools
- Go to Network tab
- Refresh the page
- Filter by "pirsch"
- Verify
pirsch.jsorpirsch-extended.jsloads successfully (Status: 200)
Check Console:
// Run in browser console
console.log(typeof pirsch !== 'undefined' ? 'Pirsch loaded successfully' : 'Pirsch not loaded');
Step 2: Test Pageview Tracking
- Visit your website
- Open Network tab in DevTools
- Filter by "pirsch.io"
- Navigate to a different page
- Look for POST requests to
api.pirsch.io/api/v1/hit - Verify request returns 200 status
Step 3: Verify in Dashboard
- Wait 5-10 minutes for data processing
- Log into Pirsch dashboard
- Go to your website's analytics
- Check for recent pageviews in real-time or hourly stats
- Verify page paths and referrers appear correctly
Step 4: Test Custom Events (Extended Script Only)
// Run in browser console
pirsch('Test Event', { meta: { test: true } });
Check Events section in dashboard after 5-10 minutes.
Common Installation Issues
Script Not Loading
Issue: Pirsch script doesn't load in Network tab
Solutions:
- Verify the script URL is correct (
https://api.pirsch.io/pirsch.js) - Check for ad blockers or privacy extensions
- Ensure your firewall isn't blocking api.pirsch.io
- Verify script tag is properly closed
No Data in Dashboard
Issue: Pageviews don't appear in Pirsch dashboard
Solutions:
- Wait 5-10 minutes for data processing
- Verify identification code is correct
- Check that domain matches what's configured in Pirsch
- Ensure you're not blocking your own visits (check dev exclusions)
Events Not Firing
Issue: Custom events don't appear
Solutions:
- Confirm you're using pirsch-extended.js, not pirsch.js
- Verify
pirsch()function is available:typeof pirsch - Check browser console for JavaScript errors
- Ensure events are in correct format with
meta: {}object
Duplicate Pageviews
Issue: Each page counts as 2 pageviews
Solutions:
- Check for duplicate script tags in HTML
- Verify you're not using both client-side and server-side tracking
- Ensure SPA route tracking isn't duplicating hits
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| Script blocked by ad blocker | Browser extension | Use server-side tracking or proxy |
| 403 Forbidden error | Wrong identification code | Verify code in Pirsch dashboard |
| Mixed content warning | HTTP on HTTPS site | Ensure script URL uses HTTPS |
| Script not executing | Missing defer/async | Add defer attribute to script tag |
| Events undefined | Basic script loaded | Replace with pirsch-extended.js |
| CSP violations | Content Security Policy | Add api.pirsch.io to CSP script-src |
| Double tracking on SPA | Manual + automatic tracking | Remove duplicate hit() calls |
| No data on localhost | Dev domain excluded | Remove localhost from data-dev attribute |
Best Practices
- Use
deferAttribute: Always usedeferfor optimal performance - Place in
<head>: Load scripts early for accurate tracking - Environment Variables: Store codes/tokens in env vars, not source code
- Dev Exclusions: Use
data-devto exclude development environments - Test Thoroughly: Verify tracking in staging before production deployment
- Monitor Errors: Set up error logging for tracking failures
- Update Regularly: Keep SDK packages up to date
- Document Setup: Record which installation method you used
Security Considerations
- Never Commit Tokens: Don't commit access tokens to version control
- Use Environment Variables: Store sensitive data in .env files
- Restrict API Tokens: Generate separate tokens for different environments
- Monitor Usage: Regularly check for unusual API activity
- HTTPS Only: Always use HTTPS for script sources