# Event Tracking

<div class="mb-6">
  <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-medium bg-orange-50 dark:bg-orange-500/10 text-orange-600 dark:text-orange-400 border border-orange-200 dark:border-orange-500/20">
    <div class="p-0.5 rounded-full bg-orange-100 dark:bg-orange-500/20">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-3 h-3">
        <path fill-rule="evenodd" d="M6.701 2.25c.577-1 2.02-1 2.598 0l5.196 9a1.5 1.5 0 01-1.299 2.25H2.804a1.5 1.5 0 01-1.3-2.25l5.197-9zM8 4a.75.75 0 01.75.75v3a.75.75 0 11-1.5 0v-3A.75.75 0 018 4zm0 8a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
      </svg>
    </div>
    <span>BETA</span>
  </span>
  <p class="text-xs text-orange-600 dark:text-orange-400 mt-2">This feature is currently in beta testing and may be subject to changes.</p>
</div>

Event tracking allows you to capture specific user interactions on your website, such as button clicks, file downloads, or form submissions. This goes beyond simple page views to help you understand how visitors are engaging with your content.

## Enabling Event Tracking

To start tracking events, you need to add the `?events` parameter to your existing Tinylytics embed script.

Update your script tag to look like this:

```html
<script src="https://tinylytics.app/embed/YOUR_UNIQUE_SITE_CODE.js?events"></script>
```

If you are already using other parameters (like `?kudos` or `?hits`), you can combine them:

```html
<script src="https://tinylytics.app/embed/YOUR_UNIQUE_SITE_CODE.js?events&kudos&hits"></script>
```

**Note:** When hit tracking is disabled (for example via your site’s settings or the `?ignore` parameter on the script URL), event tracking will not trigger either. Events are only sent when hit collection is enabled.

## How to Track Events

Once the script is updated, you can track events by adding specific `data-` attributes to any HTML element on your page (links, buttons, divs, etc.).

The script listens for clicks on these elements and automatically sends the event data to Tinylytics.

### Basic Usage

Use the `data-tinylytics-event` attribute to define the event name.

```html
<button data-tinylytics-event="button.subscribe">
  Subscribe
</button>
```

### Event Naming Convention

Event names **must** follow the `category.action` format.

*   **Good:** `button.click`, `file.download`, `form.submit`, `video.play`
*   **Bad:** `click`, `download`, `submit` (missing category)

The script will automatically convert arrows `->` to dots `.`, so `button->click` becomes `button.click`.

### Adding Event Values

You can optionally provide a value for the event using `data-tinylytics-event-value`. This is useful for distinguishing between similar events, like downloading different files.

```html
<a href="/guides/pricing.pdf" 
   data-tinylytics-event="file.download" 
   data-tinylytics-event-value="pricing-guide.pdf">
  Download Pricing Guide
</a>
```

## Examples

### Tracking File Downloads

```html
<a href="/assets/whitepaper.pdf" 
   data-tinylytics-event="download.whitepaper"
   data-tinylytics-event-value="v1.0">
  Get Whitepaper
</a>
```

### Tracking External Links

```html
<a href="https://twitter.com/tinylytics" 
   data-tinylytics-event="social.twitter"
   target="_blank">
  Follow us on Twitter
</a>
```

### Tracking Form Submissions

You can add the attribute to the submit button of a form:

```html
<form action="/subscribe" method="POST">
  <input type="email" name="email">
  <button type="submit" data-tinylytics-event="form.newsletter_signup">
    Join Newsletter
  </button>
</form>
```

## Advanced Configuration

### Using Beacons vs. Fetch

By default, Tinylytics uses the standard `fetch` API to send event data. This is reliable for most interactions. However, for links that navigate away from the current page (like external links), the browser might cancel the request before it completes.

To ensure events are sent even when the user leaves the page, you can enable **Beacon** mode by adding `?beacon` to your script URL.

```html
<script src="https://tinylytics.app/embed/YOUR_UNIQUE_SITE_CODE.js?events&beacon"></script>
```

This uses `navigator.sendBeacon`, which queues the data to be sent asynchronously by the browser, ensuring it reaches our servers even if the page unloads immediately.

**Note:** Beacons may be blocked by some privacy-focused browsers (like Brave) or ad-blocking extensions, as they are sometimes associated with third-party tracking. If 100% data completeness is critical and your audience uses these tools heavily, consider testing which method works best for your specific use case.


### Debouncing

To prevent accidental double-clicks from skewing your data, the script automatically "debounces" events. If a user clicks the same element multiple times rapidly, only one event will be recorded every 500 milliseconds.