You need to create a Google Analytics account first. This guide will walk you through the entire process.
After creating the stream, you'll see a Measurement ID that looks like: G-XXXXXXXXXX
Copy this ID - you'll need it for the next step.
Add this code to EVERY HTML page, right after the <head> tag:
<!-- Google Analytics 4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX', {
page_path: window.location.pathname,
});
</script>
<!-- End Google Analytics 4 -->
⚠️ Replace G-XXXXXXXXXX with your actual Measurement ID
Add this enhanced tracking code to track affiliate link clicks:
<script>
// Track affiliate link clicks
document.addEventListener('DOMContentLoaded', function() {
// Find all Amazon affiliate links
const affiliateLinks = document.querySelectorAll('a[href*="amazon.com"]');
affiliateLinks.forEach(link => {
link.addEventListener('click', function() {
// Send event to GA4
gtag('event', 'affiliate_click', {
'affiliate_network': 'Amazon',
'item_name': this.closest('.card')?.querySelector('h3')?.textContent || 'Unknown',
'link_url': this.href,
'page_location': window.location.pathname
});
});
});
// Track scroll depth
let scrollDepths = [25, 50, 75, 90];
let scrolled = [];
window.addEventListener('scroll', function() {
let scrollPercent = (window.scrollY + window.innerHeight) / document.body.scrollHeight * 100;
scrollDepths.forEach(depth => {
if (scrollPercent >= depth && !scrolled.includes(depth)) {
gtag('event', 'scroll', {
'percent_scrolled': depth,
'page_location': window.location.pathname
});
scrolled.push(depth);
}
});
});
// Track time on page
let startTime = Date.now();
window.addEventListener('beforeunload', function() {
let timeSpent = Math.round((Date.now() - startTime) / 1000);
gtag('event', 'time_on_page', {
'time_seconds': timeSpent,
'page_location': window.location.pathname
});
});
});
</script>
In Google Analytics, mark these events as conversions:
Set up audiences for remarketing:
Create custom reports to track performance:
Since you have a Privacy Policy, make sure it mentions: