Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hey everyone,
since there's no hint in the docs that this is doable, I'm afraid I'll have to live with a No, but maybe there's some undocumented possibility to achieve this:
Is it possible to get the state of the Consent API in Liquid as well?
I want to load external tracking scripts only when tracking consent is given and/or tracking is generally allowed. Shopify's native Google Analytics implementation appears to do exactly this, for example. So I'd like to do something like this in my theme liquid:
{% if visitor.can_be_tracked %} <script async src="https://url.to/trackingscript.js"></script> {% endif %}
(this obviously doesn't work)
It is possible to muddle something together in JavaScript, essentially appending the <script> element dynamically to the DOM after querying the Consent API state, but I'd find it more elegant not to bother the browser/client at all with this.
Kind regards
Nico
Hey Nico,
quick question. Did you ever solve this? One of our clients wants to implement server side tagging on their site and Shopify plugins seem to be very "idependent". Plugin A doesn't know about what plugin B is doing. So my problem is: How can I run a script depending on how the consent decision has been made? How did you solve this? Did you ever find a solution?
We might want to share the solution here for other people to find.
Andeas
Hey there,
I'm afraid not, we have to go the JavaScript way. What I did:
Somewhere below the {{ content_for_header }} line in theme.liquid (or, if you don't want to meddle in your theme's code, create a "Custom Liquid" block in your header or footer section), inserted this code:
<script>
function msClarity() {
userCanBeTracked = window.Shopify.customerPrivacy.analyticsProcessingAllowed();
if(userCanBeTracked) {
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "YOURTAGID");
}
}
function bingConv() {
userCanBeTracked = window.Shopify.customerPrivacy.thirdPartyMarketingAllowed();
if(userCanBeTracked) {
(function(w,d,t,r,u)
{
var f,n,i;
w[u]=w[u]||[],f=function()
{
var o={ti:"YOURTAGID", enableAutoSpaTracking: true};
o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")
},
n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function()
{
var s=this.readyState;
s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)
},
i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)
})
(window,document,"script","//bat.bing.com/bat.js","uetq");
}
}
window.Shopify.loadFeatures([
{
name: 'consent-tracking-api',
version: '0.1',
}
],
function(error) {
if (error) {
throw error;
}
msClarity();
bingConv();
document.addEventListener('visitorConsentCollected', () => {
msClarity();
bingConv();
});
});
</script>
A few notes:
Feel free to use and/or refine this - I'm no developer, so please forgive my rather messy approach here. 🙂
Update: The thirdPartyMarketingAllowed() method is now called marketingAllowed(), so this is aforementioned code with the proper methods provided by the API, properly working as of the time of this post:
<script>
function msClarity() {
let userCanBeTracked = window.Shopify.customerPrivacy.analyticsProcessingAllowed();
if(userCanBeTracked) {
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "[REDACTED]");
}
}
function bingConv() {
let userCanBeTracked = window.Shopify.customerPrivacy.marketingAllowed();
if(userCanBeTracked) {
(function(w,d,t,r,u) {
var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"[REDACTED]", enableAutoSpaTracking: true};
o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")
},
n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function()
{
var s=this.readyState;
s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)
},
i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)
})
(window,document,"script","//bat.bing.com/bat.js","uetq");
}
}
window.Shopify.loadFeatures([
{
name: 'consent-tracking-api',
version: '0.1',
}
],
function(error) {
if (error) {
throw error;
}
msClarity();
bingConv();
document.addEventListener('visitorConsentCollected', () => {
msClarity();
bingConv();
});
});
</script>
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024