Created
March 25, 2018 00:17
-
-
Save craigpotter/2e0c19db74fd1c403cf1e402d7050259 to your computer and use it in GitHub Desktop.
Dynamic Remarketing For Wordpress (New GTag)
This file contains 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
function cp_dynamic_remarketing_tag() | |
{ | |
global $woocommerce; | |
$adword_code = 'AW-00000001'; | |
if(is_front_page() || is_home()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_pagetype': 'home' | |
}); | |
</script> | |
<?php | |
} | |
// Check if it is a product category page and set the category parameters. | |
elseif (is_product_category()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_pagetype': 'category', | |
'ecomm_category': '<?php echo single_term_title();?>' | |
}); | |
</script> | |
<?php | |
} | |
// Check if it a search results page and set the searchresults parameters. | |
elseif (is_search()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_pagetype': 'searchresults' | |
}); | |
</script> | |
<?php | |
} | |
// Check if it is a product page and set the product parameters. | |
elseif (is_product()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_pagetype': 'product', | |
'ecomm_prodid': '<?php echo get_the_ID(); ?>', | |
'ecomm_totalvalue': <?php | |
$product = get_product( get_the_ID() ); | |
echo str_replace(",","",$product->get_price()); | |
?>, | |
'ecomm_category': '<?php echo single_term_title();?>' | |
}); | |
</script> | |
<?php | |
} | |
// Check if it is the cart page and set the cart parameters. | |
elseif (is_cart()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_prodid': <?php | |
echo "["; | |
$cartprods = $woocommerce->cart->get_cart(); | |
$cartprods_items = array(); | |
foreach((array)$cartprods as $entry){ | |
array_push($cartprods_items, "'" . $entry['product_id'] . "'"); | |
} | |
echo implode(', ', $cartprods_items); | |
echo "]"; | |
?>, | |
'ecomm_pagetype': 'cart', | |
'ecomm_totalvalue': <?php echo str_replace(",","",number_format($woocommerce->cart->total,2)); ?> | |
}); | |
</script> | |
<?php | |
} | |
// Check if it the order received page and set the according parameters | |
elseif (is_order_received_page()){ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_prodid': <?php | |
echo "["; | |
$order = new WC_Order(wc_get_order_id_by_order_key($_GET['key'])); | |
$order_total = $order->get_total(); | |
$items = $order->get_items(); | |
$order_items = array(); | |
foreach((array)$items as $item){ | |
array_push($order_items, "'" . $item['product_id'] . "'"); | |
} | |
echo implode(', ', $order_items); | |
echo "]"; | |
?>, | |
'ecomm_pagetype': 'purchase', | |
'ecomm_totalvalue': <?php echo $order_total; ?> | |
}); | |
</script> | |
<?php | |
} | |
// For all other pages set the parameters for other. | |
else{ | |
?> | |
<script type="text/javascript"> | |
gtag('event', 'page_view', { | |
'send_to': '<?php echo $adword_code;?>', | |
'ecomm_pagetype': 'other' | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action('wp_footer', 'cp_dynamic_remarketing_tag'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add to theme's function.php
Also make sure you have the gtag code in the header