I am trying to do a .each function to push an impression for each product on a search results page into a dataLayer. I am having a hard time figuring out how to specify the $(this) element in my foreach. I am not sure this is best way to do what I want, so if anyone has a better way of doing this, please let me know :)
Hope I am making sense! Thanks!
<script>
$(function () {
$(".result-price-carpark-name span, .result-price-actual").each(function(index) {
console.log( index + ": " + $( this ).text() );
var carpark = $(this).text(); //How to specify its $(this) inside the .result-price-carpark-name span??
var price = $(this).text(); //How to specify its $(this) inside the .result-price-actual??
dataLayer.push({
'ecommerce': {
'currencyLocal': 'USD',
'impressions': [{
'name': carpark,
'id': carpark,
'price': price,
'category': carpark,
'list': 'ParkAstro',
'position': ''
}]
}
})
})
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="result-prices-item">
<div class="result-price-carpark-name"><span>PARK1</span></div>
<div class="result-price-actual">22</div>
</div>
<div class="result-prices-item">
<div class="result-price-carpark-name"><span>PARK2</span></div>
<div class="result-price-actual">22</div>
</div>
<div class="result-prices-item">
<div class="result-price-carpark-name"><span>PARK3</span></div>
<div class="result-price-actual">22</div>
</div>