As another solution based on the discussion in the comments you may be want to place something like this in your Minicart template (app/design/frontend/[theme]/[theme]/template/checkout/cart/minicart/items.phtml
).
Please note, that this snippet never really got optimized nor revised as it was a quick fix in a particular situation some long time ago.
<!-- REMARKETING -->
<?php $conversionId = "YOUR CONVERSION ID"; // place your conversion id here ?>
<?php
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
$productReg = Mage::registry('current_product');
$categoryReg = Mage::registry('current_category');
$ecomm_category = null;
$ecomm_prodid = null;
if(Mage::app()->getFrontController()->getAction()->getFullActionName() === 'checkout_onepage_success'):
$ecomm_pagetype = 'purchase';
$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
foreach ($order->getAllItems() as $item):
$itemIds[] = "'".$item->getSku()."'";
endforeach;
$ecomm_prodid = "[".implode(",", $itemIds)."]";
elseif($routeName == 'cms' && $identifier == 'home'):
$ecomm_pagetype = 'home';
$ecomm_prodid = null;
elseif($routeName == 'checkout'):
$ecomm_pagetype = 'cart';
$itemIds = array();
$quote = Mage::helper('checkout/cart')->getQuote();
if($quote->getItemsCount()):
foreach ($quote->getAllItems() as $item):
$itemIds[] = "'".$item->getSku()."'";
endforeach;
$ecomm_prodid = "[".implode(",", $itemIds)."]";
else:
$ecomm_prodid = null;
endif;
elseif($routeName == 'catalogsearch'):
$ecomm_pagetype = 'searchresults';
$ecomm_prodid = null;
elseif($routeName == 'catalog'):
if($categoryReg):
$ecomm_category = $categoryReg->getName();
endif;
if($productReg):
$ecomm_pagetype = 'product';
$ecomm_prodid = "'".$productReg->getSku()."'";
else:
$ecomm_pagetype = 'category';
$ecomm_prodid = null;
endif;
else:
$ecomm_pagetype = 'other';
$ecomm_prodid = null;
endif;
if($ecomm_pagetype == 'purchase'):
$ecomm_totalvalue = $order->getGrandTotal();
elseif($ecomm_pagetype == 'cart'):
$ecomm_totalvalue = Mage::helper('checkout/cart')->getQuote()->getGrandTotal();
if($ecomm_totalvalue == null):
$ecomm_totalvalue = 0;
endif;
elseif($ecomm_pagetype == 'product'):
$ecomm_totalvalue = $productReg->getFinalPrice();
else:
$ecomm_totalvalue = null;
endif;
?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: <?php echo "'".$ecomm_pagetype."'"; ?>,
<?php if($ecomm_prodid != null): ?>
ecomm_prodid: <?php echo $ecomm_prodid; ?>,
<?php endif; ?>
<?php if($ecomm_category != null): ?>
ecomm_category: <?php echo "'".$ecomm_category."'"; ?>,
<?php endif; ?>
<?php if($ecomm_totalvalue != null): ?>
ecomm_totalvalue: <?php echo $ecomm_totalvalue; ?>,
<?php endif; ?>
};
</script>
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = <?php echo $conversionId; ?>;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/<?php echo $conversionId; ?>/?guid=ON&script=0"/>
</div>
</noscript>
Even if alert(totalvalue);
for example would return 100
it most likey is still just a string rather than a number. You can check that by alert(typeof(totalvalue)); // expected result in this case is 'string'
.
So you will have to convert that string to a number using something like totalvalue = Number($j('#valor_produto').val());
or 'ecomm_totalvalue': Number(totalvalue)
. If you now alert(typeof(totalvalue));
you should get prompted number
.
So, for example:
(function(){
try {
url_atual = $j('#urlAtual').val();
alert(url_atual);
if(url_atual == "catalog"){
var prodid, totalvalue;
//page product/category
totalvalue = Number($j('#valor_produto').val()); // forcing type "number" rather than default "string"
prodid = $j('#id_produto').val();
alert(totalvalue);
alert(prodid);
dataLayer.push({
'event': 'fireRemarketingTag',
google_tag_params: {
'ecomm_prodid': prodid,
'ecomm_pagetype': 'product',
'ecomm_totalvalue': totalvalue // added missing quotes
}
});
}
else if (url_atual == "ajax") {
var prodid, totalvalue;
//cart
console.log('entrou carrinho');
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': prodid,
'ecomm_pagetype': 'cart',
'ecomm_totalvalue': totalvalue
}
});
}
else if (url_atual == "cms") {
var prodid, totalvalue;
//other pages/cms
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': prodid,
'ecomm_pagetype': 'purchase',
'ecomm_totalvalue': totalvalue
}
});
}
else {
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': { }
});
}
}
catch (err) {}
}) ();
For easy debugging:
One easy way to test google-snippet integrations is using the Tag Assistant (by Google)
Google Chrome extension (Tag Assistant on chrome.google.com).
General tag type definitions for Google Remarketing:
Example for retailers (more types linked in the source):
Custom parameter Sample values Definition
--------------------------------------------------------------------------
ecomm_prodid* 1234 The product ID parameter allows your dynamic ads to show people the exact product they viewed on your website.
ecomm_pagetype** home, The page type parameter collects which pages people visited on your website.
searchresults,
category, To use automatically created lists, this value must be in English, even if your site is in a different language.
product,
cart, purchase,
other
ecomm_totalvalue** 49.99 The total product value parameter is the value of the product. On cart pages with multiple products, total product value is the sum of all items someone has selected.
*Required
**Recommended
Source: AdWords Help on support.google.com
ecomm_totalvalue
anddynx_totalvalue
as the error messages states that you have the passed values quoted (i.e.ecomm_totalvalue: '100.00'
does not work, where elseecomm_totalvalue: 100.00
does).