I have an e-commerce store powered by WHMCS, and I have a Google Ads campaign I'd like to set up conversion tracking for.
Google Ads has given me the following code to track conversions on my WHMCS site.
<script>
gtag('event', 'conversion', {
'send_to': 'AW-xxx/xxx',
'value': 10.0,
'currency': 'AUD',
'transaction_id': ''
});
</script>
During the Google Ads conversion setup, they asked for a default conversion value which I set to AU$10.
I've added that conversion script to the complete.tpl
file.
How do I dynamically set the value assigned to the Google Ads conversion based on the order that has just been made in on the order complete page?
I have the Google Analytics WHMCS app installed and configured.
I see there is no WHMCS Google Ads app.
Not sure about the Google Ads
, but I integrated Google Analytics 4
before stumbling upon the WHMCS app. So you can follow the same for Google Ads
First off, create a custom hook file in includes\hooks
In the file, create a hook
for defining Google Ads calling script in the head
tag (For further details, please see ClientAreaHeadOutput):
add_hook('ClientAreaHeadOutput', 1, function ($vars) {
return '<script>Google Ads Script Here</script>';
});
Add another hook for the noscript
code in start of the body
tag, if there is any (For further details, please see ClientAreaHeaderOutput):
add_hook('ClientAreaHeaderOutput', 1, function ($vars) {
return "<noscript>No Script Code Here</noscript>";
});
Add another hook that will mark conversion
when the invoice is paid, I have customized it to what you require (For further details, please see InvoicePaid):
add_hook('InvoicePaid', 1, function ($vars) {
$invoiceID = $vars['invoiceid'];
$order = Capsule::table('tblorders')->where('invoiceid', '=', $invoiceID)->first();
if ($order !== null) { // order exists
$transactionID = $order->id;
$orders = localAPI('GetOrders', array('id' => $transactionID));
$order = $orders['orders']['order'][0];
$result = localAPI('GetClientsDetails', array('clientid' => $order['userid']));
$client = $result['client'];
$currencyCode = $client["currency_code"];
$total = 0;
foreach ($order['lineitems']['lineitem'] as $product) {
// get actual product pricing
$price = (float)preg_replace("/[^\\d.]+/", "", $product['amount']);
if ($product['type'] == 'product') {
$tempItem = Capsule::table('tblhosting')->where('id', '=', $product['relid'])->first();
if ($tempItem != NULL) {
$actualProduct = getProduct($product['pid']); // fetch original product for actual pricing
$selectedCycle = strtolower($product['billingcycle']);
$price = (float)$actualProduct['pricing'][$currencyCode][$selectedCycle];
}
}
$total += $price;
}
return "
<script>
gtag('event', 'conversion', {
'send_to': 'AW-xxx/xxx',
'value': " . (float)$total . ",
'currency': '" . $currencyCode . "',
'transaction_id': '" . $transactionID . "'
});
</script>";
}
});
Also don't forget to include the Capsule
on top of the file as:
use Illuminate\Database\Capsule\Manager as Capsule;