Last active
July 4, 2017 11:25
-
-
Save tankbar/c534736e2ffe0ed6316378fed828a455 to your computer and use it in GitHub Desktop.
Criteo tracking on Thank you page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Criteo Thank you page - Sales Confirmation page | |
add_action( 'woocommerce_thankyou', 'criteo_tracking_cart', 80 ); | |
function criteo_tracking_cart( $order_id ) { | |
$order = new WC_Order( $order_id ); | |
$order_nr = $order->get_order_number(); | |
$order_email = get_user_meta( $order->user_id, 'billing_email', true ); | |
$source_address = $order_email; | |
$processed_address = strtolower($source_address); //convert address to lower case | |
$processed_address = trim($processed_address); //trimming leading and trailing spaces | |
$processed_address = mb_convert_encoding($processed_address, "UTF-8", "ISO-8859-1"); //conversion from ISO-8859-1 to UTF-8 (replace "ISO-8859-1" with the source encoding of your string) | |
$processed_address = md5($processed_address); //hash address with MD5 algorithm | |
?> | |
<!-- Criteo tracking conversation script --> | |
<script type="text/javascript"> | |
var deviceType = /iPad/.test(navigator.userAgent) ? "t" : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent) ? "m" : "d"; | |
window.criteo_q = window.criteo_q || []; | |
window.criteo_q.push( | |
{ event: "setAccount", account: XXXXX }, | |
{ event: "setEmail", email: "<?php echo $processed_address; ?>" }, | |
{ event: "setSiteType", type: deviceType }, | |
{ event: "trackTransaction", id: "<?php echo $order_nr; ?>", item: [<?php | |
// This is how to grab line items from the order | |
$line_items = $order->get_items(); | |
// This loops over line items | |
foreach ( $line_items as $item ) { | |
if ($item['variation_id']) { | |
$price = get_post_meta($item['variation_id'] , '_price', true); | |
} else { | |
$price = get_post_meta($item['product_id'] , '_price', true); | |
} | |
?> | |
{ id: "<?php echo 'woocommerce_gpf_'.$item["product_id"]; ?>", price: <?php echo $price; ?>, quantity: <?php echo $item["qty"]; ?> }, | |
<?php | |
} ?>]} | |
); | |
</script> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment