Skip to content

Instantly share code, notes, and snippets.

@smhmic
Created July 23, 2020 14:41
Show Gist options
  • Save smhmic/7e8675b564075c9ff3ef8f6c96f6f33c to your computer and use it in GitHub Desktop.
Save smhmic/7e8675b564075c9ff3ef8f6c96f6f33c to your computer and use it in GitHub Desktop.
GTM AMP Container Proxy (for sharing GA Client ID with non-AMP pages) - for uti.edu
<?php
// Callback for the GET request
function gtmAmpProxy_serve(){
//error_reporting(0);
// Get the hostname of the request origin, and parse it for the pure domain name.
$domain = explode( ':', $_SERVER['HTTP_HOST'] )[0];
if( !( $cookieDomain = @$_GET['cookieDomain'] ) ){
$cookieDomain = str_replace( 'www.', '', strtolower( $domain ) );
if( substr( $cookieDomain, -8 ) === '.uti.edu' ){
$cookieDomain = 'uti.edu';
}
}
// Check if the browser already has the _ga cookie. If not, generate a new cookie.
$cid = $_COOKIE['_ga'];
if( !isset( $cid ) ){
$domainLength = count( explode( '.', $cookieDomain ) );
$cid = "GA1.{$domainLength}.".rand( 100000000, 999999999 ).'.'.time();
}
// Store the actual Client ID (last two numbers) of the _ga cookie value in the $cidNumber variable
$cidNumber = preg_replace( '/^GA.\.[^.]+\./', '', $cid );
// Fetch the actual GTM container, by passing the valid query parameters from the original request.
$container = file_get_contents( "https://www.googletagmanager.com/amp.json?$_SERVER[QUERY_STRING]" );
// Replace the &cid; parameter value with ${clientId}
$container = preg_replace( '/(&cid=)[^&]+/', '${1}${clientId}', $container );
// Add the clientId to the "vars" object in the container JSON.
$container = json_decode( $container );
$container->vars->clientId = $cidNumber;
// Build a new HTTP response from the modified configuration file.
$response = json_encode( $container );
// Clear output that could create an invalid response (i.e. PHP warnings).
@ob_clean();
// Clear any existing response headers.
header_remove();
// Add the required headers (Set-Cookie, most importantly) to the Request
http_response_code( 200 );
header( "Set-Cookie: _ga={$cid}; Domain={$cookieDomain}; Path=/; Secure; SameSite=None; "
."Expires=".date( 'D, j F Y H:i:s', time()+60*60*24*365*2)." GMT;" );
header( 'Access-Control-Allow-Origin: https://cdn.ampproject.org' );
header( "AMP-Access-Control-Allow-Source-Origin: https://{$domain}" );
header( 'Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin' );
header( 'Content-Type: application/json; charset=utf-8' );
// Return the HTTP response.
echo $response;
exit();
}
gtmAmpProxy_serve();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment