📊 Page View Counter Examples

This page demonstrates how to display view counts using the page view tracking script.

Basic View Counter

Simply add an element with data-page-views attribute:

<div data-page-views>Loading...</div>
Loading view count...

Custom Format with Text

Use data-format to customize the display format with {views} and {domain} placeholders:

<span data-page-views data-format="👀 {views} views"></span>
👁️ Loading...

Detailed Format with Domain

Include domain information in your view counter:

<p data-page-views data-format="{domain} has been viewed {views} times"></p>

Loading...

Using CSS Classes

You can also use CSS classes instead of data attributes:

<div class="page-views">Views: Loading...</div>
<span id="view-count">Loading...</span>
Views: Loading...
Loading count...

📋 Available Selectors

The script automatically looks for elements with any of these selectors:

🎨 Format Options

Use the data-format attribute to customize how the count is displayed:

Examples:

⚙️ Setup Instructions

To use view counters on your website:

  1. Include the tracking script in your HTML:
<script src="https://hard-house.club/track.js"></script>
  1. Add view counter elements anywhere on your page using the examples above
  2. The script will automatically update the counters when the page loads and after each view is tracked

🔧 How It Works

📊 Manual API Calls

You can also fetch view counts directly without using the automatic counter:

Custom JavaScript Implementation

// Get views for current domain
async function getCurrentViews() {
  const domain = window.location.hostname;
  const response = await fetch(`https://hard-house.club/api/views/${domain}`);
  const data = await response.json();
  return data.views;
}

// Update a custom element
getCurrentViews().then(views => {
  document.getElementById('my-counter').textContent = `${views} views`;
});

Track a Custom Event

// Manually track a page view
fetch('https://hard-house.club/api/track', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ url: window.location.href })
});

API Response Format

{
  "domain": "example.com",
  "views": 42,
  "message": "Page view tracked successfully"
}