Skip to content

Instantly share code, notes, and snippets.

@mbaersch
Created March 25, 2020 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaersch/795a9c3ab22941b175f36c9b6c72955b to your computer and use it in GitHub Desktop.
Save mbaersch/795a9c3ab22941b175f36c9b6c72955b to your computer and use it in GitHub Desktop.
send serverside conversions from a Wordpress shop (e. g. wooCommerce) to Google Analytics
<?php
// add one of the two following options to the functions.php of your child theme in order to send server side
// conversion data to Google Analytics
// NOTE: function store_gclid() is used by both variants
/***************************************************************************************************/
// send conversion to Analytics - Option 1: track success page as only pageview in the session and
// create a goal for path "/conversion/"
// adjust goal path (fragment) in $goalurl in order to fit your url structure
// comment out line below to deactivate
/***************************************************************************************************/
add_action ('wp_head','trackPages');
function trackPages() {
//store gclid on landing page if present
store_gclid();
$gclid = $_SESSION['gclid'];
$dp = $_SERVER['REQUEST_URI'];
$goalurl = '/shop/checkout/order-received/';
$isTrackPage = (strpos($dp, $goalurl) > -1);
//track hit for conversion page if success page is loaded
if (($isTrackPage === TRUE) && (isset($gclid) && $gclid !== "")) {
$cid = $gclid;
$dh = $_SERVER['SERVER_NAME'];
$dp = urlencode("/conversion/?gclid=".$gclid);
//Test:
$dl = urlencode('https://'.$_SERVER['SERVER_NAME']."/conversion/?gclid=".$gclid);
$pg_url = "https://www.google-analytics.com/collect?v=1&dh=$dh&dl=$dl&t=pageview&tid=UA-143498129-2&cid=$cid&aip=1";
$ch = cURL_init();
curl_setopt($ch, CURLOPT_URL, $pg_url);
cURL_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
cURL_setopt($ch, CURLOPT_FRESH_CONNECT, true);
cURL_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
curl_exec($ch);
cURL_close($ch);
return true;
}
}
function store_gclid() {
session_start(['cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => true]);
$gclid = $_GET['gclid'];
if (isset($gclid) && $gclid !== "") {
$_SESSION['gclid'] = $gclid;
}
}
/***************************************************************************************************/
// send conversion to Analytics - Option 2: use woocommerce hook and send event incl. sales amount
// uncomment both lines below to activate
/***************************************************************************************************/
//add_action('woocommerce_thankyou', 'serverside_conversion_event');
//add_action ('wp_head','store_gclid');
function send_conversion_event($t_ua, $t_ea, $t_vl) {
$gclid = $_SESSION['gclid'];
if (isset($gclid) && $gclid !== "") {
$cid = $gclid;
$dh = $_SERVER['SERVER_NAME'];
$dp = urlencode("/conversion/?gclid=".$gclid);
$t_ea = urlencode($t_ea);
$event_url = "https://www.google-analytics.com/collect?v=1&dp=$dp&dh=$dh&t=event&tid=$t_ua&cid=$cid&ec=Conversion&ea=$t_ea&ev=$t_vl&ni=0&aip=1";
$ch = cURL_init();
curl_setopt($ch, CURLOPT_URL, $event_url);
cURL_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
cURL_setopt($ch, CURLOPT_FRESH_CONNECT, true);
cURL_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
curl_exec($ch);
cURL_close($ch);
return true;
} else return false;
}
function serverside_conversion_event($order_id) {
$order = wc_get_order($order_id);
send_conversion_event("UA-143498129-2", "Abschluss", $order->get_total());
}
?>
@mbaersch
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment