Reputation: 101
I want to push a dataLayer on an eccommerce site for Dynamic remarketing. My code sits under some previous dataLayer pushed they had in place:
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
jQuery.ajax({
url: "https://www.ecample.com/gauncached/index/gaIsLoggedIn/"
}).done(function(response) {
dataLayer.push({
'Logged In': response
});
});
jQuery.ajax({
url: "https://www.example.com/gauncached/index/hasCustomerPurchased/"
}).done(function(response) {
dataLayer.push({
'Is Purchaser': response
});
});
<script type="text/javascript">
var dataLayer = window.dataLayer || [];
dataLayer.push({
'ecomm_prodid': ['04-04-542-399-02-02-50'],
'ecomm_pagetype' : 'cart',
'ecomm_totalvalue' : 615.00 });
</script>
This is not being recognised in my debug. Is it because I have multiple dataLayer's?
Upvotes: 0
Views: 1157
Reputation: 1418
Your code is probably not being recognized because of JS error. You cannot have HTML tag within JS code block, so just remove like this:
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
jQuery.ajax({
url: "https://www.ecample.com/gauncached/index/gaIsLoggedIn/"
}).done(function(response) {
dataLayer.push({
'Logged In': response
});
});
jQuery.ajax({
url: "https://www.example.com/gauncached/index/hasCustomerPurchased/"
}).done(function(response) {
dataLayer.push({
'Is Purchaser': response
});
});
//note that there is no need to initialize dataLayer again
dataLayer.push({
'ecomm_prodid': ['04-04-542-399-02-02-50'],
'ecomm_pagetype' : 'cart',
'ecomm_totalvalue' : 615.00
});
</script>
"This is not being recognised in my debug. Is it because I have multiple dataLayer's?" - you don't have multiple dataLayer's in the first place. With your code:
var dataLayer = window.dataLayer || [];
You are checking if the dataLayer is already declared, which it is so you are referencing the same dataLayer. You can have a look at simliar question here: https://stackoverflow.com/a/1961539/7064943 ... I hope this helps
Upvotes: 1