Plausible Analytics Integration

  • JNext lesson
  • KPrevious lesson
  • FSearch lessons
  • EscClear search

Send redirect click events to Plausible Analytics via their Events API. This server-side integration bypasses ad blockers and requires no client-side JavaScript, giving you accurate click tracking in your privacy-friendly Plausible dashboard.

Requirements

  • GT Link Manager 1.5+ installed and active
  • A Plausible Analytics account (cloud or self-hosted)
  • Your site domain registered in Plausible

Installation

  • Add the snippet below to your theme’s functions.php or a site-specific plugin
  • Replace example.com with the domain registered in your Plausible dashboard
  • If self-hosting Plausible, update the endpoint URL accordingly

The Code

<?php
/**
 * GT Link Manager - Plausible Analytics Integration
 *
 * Tracks every redirect click as a custom event in Plausible via the Events API.
 * Server-side — bypasses ad blockers and works without JavaScript.
 *
 * Setup:
 * 1. Set GTLM_PLAUSIBLE_DOMAIN to the domain registered in your Plausible dashboard.
 * 2. (Optional) If self-hosting Plausible, update GTLM_PLAUSIBLE_ENDPOINT.
 *
 * Event name: "Link Click"
 * Custom properties: link_name, link_slug, target_url, redirect_type, category_id
 *
 * @see https://plausible.io/docs/events-api
 */

defined( 'ABSPATH' ) || exit;

// ── Configuration ────────────────────────────────────────────────────────────
define( 'GTLM_PLAUSIBLE_DOMAIN',   'example.com' );                            // Your site domain in Plausible
define( 'GTLM_PLAUSIBLE_ENDPOINT', 'https://plausible.io/api/event' );          // Change if self-hosted
// ─────────────────────────────────────────────────────────────────────────────

add_action( 'gtlm_before_redirect', 'gtlm_plausible_track_click', 10, 4 );

/**
 * Send a custom event to Plausible when a redirect fires.
 *
 * @param array  $link       Link data.
 * @param string $target_url Destination URL.
 * @param int    $status     HTTP status code.
 * @param array  $headers    Response headers.
 */
function gtlm_plausible_track_click( array $link, string $target_url, int $status, array $headers ): void {

	if ( GTLM_PLAUSIBLE_DOMAIN === 'example.com' ) {
		return; // Not configured yet.
	}

	// Build the URL the user visited (the short link).
	$request_url = home_url( sanitize_text_field( $_SERVER['REQUEST_URI'] ?? '/' ) );

	$payload = [
		'name'   => 'Link Click',
		'url'    => $request_url,
		'domain' => GTLM_PLAUSIBLE_DOMAIN,
		'props'  => [
			'link_name'     => $link['name'] ?? '',
			'link_slug'     => $link['slug'] ?? '',
			'target_url'    => $target_url,
			'redirect_type' => (string) $status,
			'category_id'   => (string) ( $link['category_id'] ?? 0 ),
		],
	];

	// Forward user context so Plausible can count unique visitors correctly.
	$forward_headers = [
		'Content-Type' => 'application/json',
	];

	if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
		$forward_headers['User-Agent'] = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
	}

	// Forward the real client IP. Plausible hashes it — it is never stored raw.
	$client_ip = gtlm_plausible_get_client_ip();
	if ( $client_ip ) {
		$forward_headers['X-Forwarded-For'] = $client_ip;
	}

	wp_remote_post(
		GTLM_PLAUSIBLE_ENDPOINT,
		[
			'body'     => wp_json_encode( $payload ),
			'headers'  => $forward_headers,
			'blocking' => false,
			'timeout'  => 1,
		]
	);
}

/**
 * Get the real client IP, respecting common proxy headers.
 *
 * @return string|null Client IP or null if unavailable.
 */
function gtlm_plausible_get_client_ip(): ?string {

	$headers = [ 'HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'REMOTE_ADDR' ];

	foreach ( $headers as $header ) {
		if ( ! empty( $_SERVER[ $header ] ) ) {
			$ip = strtok( sanitize_text_field( $_SERVER[ $header ] ), ',' );
			if ( filter_var( trim( $ip ), FILTER_VALIDATE_IP ) ) {
				return trim( $ip );
			}
		}
	}

	return null;
}

How It Works

  • Hooks into gtlm_before_redirect to capture every redirect event
  • Sends a Link Click custom event to Plausible’s Events API
  • Forwards the visitor’s User-Agent and IP (via X-Forwarded-For) so Plausible can count unique visitors accurately
  • Plausible hashes the IP internally — it is never stored in raw form
  • Tracks custom properties: link_name, link_slug, target_url, redirect_type, and category_id

Configuration Notes

Events appear in your Plausible dashboard under Goal Conversions. You may need to add Link Click as a custom goal in your site settings to see it. Custom properties are available in the goal breakdown.

If you use Plausible’s self-hosted edition, change the GTLM_PLAUSIBLE_ENDPOINT constant to point to your instance (e.g., https://analytics.yourdomain.com/api/event).