Goal: is to Push the JSON to google tag manager..
Problem is: there is no available liquid variable for ‘referrer’ as per Shopify Documentation.
Possible Solution: is to create a variable using Javascript
So what I did is:
-
I created a script that will append the URL website where the visitors came from.
-
capture the appended website URL and convert it to variable
-
Put the variable
{{ ref }}
to the JSON
When I inspected the element the ‘referrer'(JSON) doesn’t have any value
please help
<script type="text/javascript">
$(document).ready(function () {
var content = document.referrer;
$(".referrer").append(content);
});
</script>
{% capture ref %}
<div class="referrer">Referrer: </div>
{% endcapture %}
<script type="text/javascript">
dataLayer.push({
‘userEmail’ : ‘{{ customer.email }}’,
‘productCategory’ : ‘{{ collection.title }}’,
‘productName’ : ‘{{ product.title }}’,
‘price’ : ‘{{ sca_price | money }}’,
‘originalPrice’ : ‘{{ sca_price | money }}’,
‘cartItems’ : ‘{{ cart.item_count }}’,
‘currency’ : ‘{{ shop.currency }}’,
‘referrer’ : ‘{{ ref }}’, // not working
‘productRating’ : ‘’,
‘reviewCount’ : null,
‘event’ : null
});
</script>
2
Answers
You don’t need a Shopify Variable to find referrer. You don’t need to even capture it in a page variable and use it. Just update:
to
Edit:
Use the below code:
Simply use JavaScript:
"referrer": document.referrer