WordPress
Integrate analytics-tube Analytics with your WordPress site
You can integrate analytics-tube into your WordPress site to track user behavior and gather analytics. There are several ways to do this depending on whether you're using a hosted WordPress.com site or a self-hosted WordPress.org installation.
We recommend using the JavaScript snippet below instead of a plain <script> tag. Some WordPress plugins and page builders strip data-* attributes from HTML, which can break tracking. The JavaScript approach avoids this issue.
Quick Start
The easiest method for most users is the WPCode plugin (formerly Insert Headers and Footers). This works on both WordPress.com (Business plan+) and self-hosted WordPress.org sites.
Install WPCode Plugin
In your WordPress dashboard, go to Plugins > Add New and search for "WPCode". Install and activate the plugin.
Alternatively, download from WordPress.org.
Add the Tracking Script
- Go to Code Snippets > Header & Footer (or Settings > Insert Headers and Footers in older versions)
- Paste this snippet into the Header section:
<script>
(function() {
var s = document.createElement('script');
s.src = 'https://app.analytics.tube/api/script.js';
s.defer = true;
s.setAttribute('data-site-id', 'YOUR_SITE_ID');
document.head.appendChild(s);
})();
</script>- Replace
YOUR_SITE_IDwith your actual Site ID from your analytics-tube dashboard - Click Save Changes
Verify Installation
Visit your site in a new browser tab, then check your analytics-tube dashboard to confirm data is being received.
Alternative Methods
Add via functions.php
For self-hosted WordPress, you can add the script via your theme's functions.php file.
Use a child theme! If you modify the parent theme's functions.php, your changes will be lost when the theme updates.
- Go to Appearance > Theme File Editor
- Select your child theme's
functions.php - Add this code at the end of the file:
function add_analytics-tube_analytics() {
?>
<script>
(function() {
var s = document.createElement('script');
s.src = 'https://app.analytics.tube/api/script.js';
s.defer = true;
s.setAttribute('data-site-id', 'YOUR_SITE_ID');
document.head.appendChild(s);
})();
</script>
<?php
}
add_action('wp_head', 'add_analytics-tube_analytics');- Replace
YOUR_SITE_IDwith your actual Site ID and save
Modify header.php
You can directly edit your theme's header.php file to add the script.
Use a child theme! Changes to the parent theme's header.php will be lost on theme updates.
- Go to Appearance > Theme File Editor
- Open
header.php - Find the closing
</head>tag and paste this script just before it:
<script>
(function() {
var s = document.createElement('script');
s.src = 'https://app.analytics.tube/api/script.js';
s.defer = true;
s.setAttribute('data-site-id', 'YOUR_SITE_ID');
document.head.appendChild(s);
})();
</script>
</head>- Replace
YOUR_SITE_IDwith your actual Site ID and save
Create a Simple Plugin
Creating a small plugin ensures your tracking code persists through theme changes.
- Connect to your site via FTP/SFTP or use a file manager plugin
- Navigate to
/wp-content/plugins/ - Create a new folder called
analytics-tube-analytics - Inside that folder, create a file called
analytics-tube-analytics.phpwith this content:
<?php
/**
* Plugin Name: analytics-tube Analytics
* Description: Adds analytics-tube Analytics tracking to your WordPress site
* Version: 1.0
*/
function analytics-tube_add_tracking_script() {
?>
<script>
(function() {
var s = document.createElement('script');
s.src = 'https://app.analytics.tube/api/script.js';
s.defer = true;
s.setAttribute('data-site-id', 'YOUR_SITE_ID');
document.head.appendChild(s);
})();
</script>
<?php
}
add_action('wp_head', 'analytics-tube_add_tracking_script');- Replace
YOUR_SITE_IDwith your actual Site ID - Go to Plugins in WordPress and activate "analytics-tube Analytics"
Troubleshooting
Script not loading
Check if the script is in the page source:
- Visit your site
- Right-click and select "View Page Source"
- Search for
analytics-tube- you should see the script tag
If the script is missing:
- Make sure you saved your changes
- Clear any caching plugins (WP Super Cache, W3 Total Cache, LiteSpeed Cache, etc.)
- Clear your CDN cache if using one (Cloudflare, etc.)
Caching plugin conflicts
Many WordPress caching plugins aggressively cache pages. After adding analytics-tube:
-
Clear all caches:
- Your caching plugin's cache
- Any CDN cache (Cloudflare, etc.)
- Browser cache (Ctrl+Shift+R or Cmd+Shift+R)
-
Exclude the script from optimization: Some optimization plugins try to defer, combine, or minify scripts. If analytics-tube isn't working, check these plugins:
- Autoptimize: Add
app.analytics.tubeto the exclusion list - WP Rocket: Exclude from JavaScript optimization
- LiteSpeed Cache: Add to JS excludes
- SG Optimizer: Exclude from combine/minify
- Autoptimize: Add
Page builder issues
If you're using a page builder like Elementor, Divi, WPBakery, or Beaver Builder, use the WPCode plugin method above rather than adding code through the page builder's custom HTML blocks. Page builders often sanitize HTML and may strip attributes from script tags.
Security plugin blocking the script
Security plugins like Wordfence or Sucuri may block external scripts. Add app.analytics.tube to your allowlist if tracking isn't working.
WordPress.com limitations
- Free/Personal/Premium plans: Cannot add custom JavaScript. You'll need to upgrade to a Business or eCommerce plan.
- Business/eCommerce plans: Use the WPCode plugin method above.
Ad blockers
Some visitors use ad blockers that block analytics scripts. This is expected behavior and will result in those visits not being tracked. If you're testing and see no data, try:
- Disabling your ad blocker temporarily
- Testing in an incognito/private window
- Testing from a different device
Self-hosted analytics-tube
If you're self-hosting analytics-tube, replace https://app.analytics.tube with your own domain:
<script>
(function() {
var s = document.createElement('script');
s.src = 'https://your-analytics-tube-domain.com/api/script.js';
s.defer = true;
s.setAttribute('data-site-id', 'YOUR_SITE_ID');
document.head.appendChild(s);
})();
</script>Tracking WooCommerce Events
If you're running a WooCommerce store, see our WooCommerce guide for tracking purchases and other e-commerce events.