UTM Parameter Passthrough

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

Automatically forward UTM parameters and tracking click IDs from the incoming short link URL to the destination. When someone clicks yoursite.com/go/deal?utm_source=twitter, the UTM parameters are preserved and appended to the final redirect URL. Essential for maintaining attribution across redirect hops.

Requirements

  • GT Link Manager 1.5+ installed and active
  • No external services needed

Installation

  • Add the snippet below to your theme’s functions.php or a site-specific plugin
  • No configuration needed — it works immediately with sensible defaults
  • Optionally customize the GTLM_UTM_PARAMS array to add or remove forwarded parameters

The Code

<?php
/**
 * GT Link Manager - UTM Parameter Passthrough
 *
 * Preserves UTM parameters (and other tracking params) from the incoming short link
 * request and appends them to the destination URL.
 *
 * Example:
 *   Visitor clicks: yoursite.com/go/deal?utm_source=twitter&utm_medium=social
 *   Destination:    partner.com/product?ref=you
 *   Final redirect: partner.com/product?ref=you&utm_source=twitter&utm_medium=social
 *
 * Supported params (configurable): utm_source, utm_medium, utm_campaign,
 * utm_term, utm_content, utm_id, ref, fbclid, gclid, msclkid
 */

defined( 'ABSPATH' ) || exit;

// ── Configuration ────────────────────────────────────────────────────────────
// Query parameters to forward. Add or remove as needed.
define( 'GTLM_UTM_PARAMS', [
	'utm_source',
	'utm_medium',
	'utm_campaign',
	'utm_term',
	'utm_content',
	'utm_id',
	'ref',
	'fbclid',
	'gclid',
	'msclkid',
	'ttclid',     // TikTok click ID
	'twclid',     // Twitter click ID
	'li_fat_id',  // LinkedIn click ID
] );
// ─────────────────────────────────────────────────────────────────────────────

add_filter( 'gtlm_redirect_url', 'gtlm_utm_passthrough', 10, 3 );

/**
 * Append incoming tracking parameters to the target URL.
 *
 * @param string $url   Target URL.
 * @param array  $link  Link data.
 * @param string $slug  Matched slug.
 * @return string Modified URL with forwarded params.
 */
function gtlm_utm_passthrough( string $url, array $link, string $slug ): string {

	$forward = [];

	foreach ( GTLM_UTM_PARAMS as $param ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( isset( $_GET[ $param ] ) && is_string( $_GET[ $param ] ) ) {
			$value = sanitize_text_field( wp_unslash( $_GET[ $param ] ) );
			if ( $value !== '' ) {
				$forward[ $param ] = $value;
			}
		}
	}

	if ( empty( $forward ) ) {
		return $url;
	}

	// Don't overwrite params already in the destination URL.
	$existing = [];
	$query    = wp_parse_url( $url, PHP_URL_QUERY );
	if ( $query ) {
		parse_str( $query, $existing );
	}

	foreach ( $forward as $key => $value ) {
		if ( ! isset( $existing[ $key ] ) ) {
			$url = add_query_arg( $key, rawurlencode( $value ), $url );
		}
	}

	return $url;
}

How It Works

  • Uses the gtlm_redirect_url filter to modify the target URL before the redirect happens
  • Scans the incoming request for recognized tracking parameters (UTM, click IDs, etc.)
  • Appends matching parameters to the destination URL using add_query_arg
  • Respects existing parameters — if the destination URL already has utm_source, the incoming value will not overwrite it
  • All values are sanitized via sanitize_text_field and URL-encoded

Supported Parameters

  • utm_source, utm_medium, utm_campaign, utm_term, utm_content, utm_id — Standard UTM parameters
  • ref — Generic referral parameter
  • fbclid — Facebook click ID
  • gclid — Google Ads click ID
  • msclkid — Microsoft Ads click ID
  • ttclid — TikTok click ID
  • twclid — Twitter (X) click ID
  • li_fat_id — LinkedIn click ID

Configuration Notes

To add or remove parameters, modify the GTLM_UTM_PARAMS constant array. For example, to also forward a custom campaign_id parameter, add it to the array.