Customer Privacy API not loading via loadFeatures

Topic summary

A developer encountered an issue where Shopify’s Customer Privacy API fails to load when called with a delay (e.g., via setTimeout). The problem specifically affects asynchronous JavaScript files, including those loaded through the Script Tags API.

Problem Details:

  • When the loadFeatures function is wrapped in setTimeout, the callback (2nd parameter) doesn’t execute with success/error responses
  • Direct loading in Liquid files works as expected
  • The issue is demonstrated through attached code screenshots comparing successful (Ex 1) vs. failed (Ex 2) implementations

Solution:
Move the setTimeout inside the callback function rather than wrapping the entire loadFeatures call:

  • Call loadFeatures immediately without delay
  • Place timing logic within the success callback function
  • This ensures the API loads properly before any delayed operations execute

A working code example is provided showing the restructured approach with initCookieBanner() containing the delayed logic.

Summarized with AI on November 2. AI used: claude-sonnet-4-5-20250929.

Hello There,

We are trying to load Customer Privacy API in store front but could not call success in some cases. The one case is when you load below code in delay its not working.
https://shopify.dev/docs/api/customer-privacy

If you load that code directly in any liquid file than its working as expected.

So in our case our app js loading in async mode so obviously it will take some delay in that case its not calling 2nd parameter of that function with error/success.

This code affecting the JS files loaded via Script tags API & have Customer Privacy API code in it.
https://shopify.dev/docs/api/admin-rest/2024-10/resources/scripttag#top

Please check attached images as below & let us know if anyone have solution on it.
https://shopify.dev/docs/api/customer-privacy

Ex 1 - Code

Ex 1 - Success

Ex 2 - Code

Ex 2 - Failed

Thanks in advance.
Viral Mandaliya

1 Like

Hello @viru ,

I guess you have already solved your problem, but this may be beneficial for others.
I also faced the same problem and thanks to your post I understood that it was caused by the setTimeout for some unknown reason.

To solve this problem you can move the setTimeout like this :

window.Shopify.loadFeatures([
    {
      name: 'consent-tracking-api',
      version: '0.1',
    }
  ],
  (error) => {
    if (error) {
      throw error;
    }
    initCookieBanner();
  }
);

function initCookieBanner() {
  setTimeout(() => {
    const currentVisitorConsent = window.Shopify.customerPrivacy.currentVisitorConsent();
    const visitorConsentsEmpty = areVisitorConsentsEmpty(currentVisitorConsent);
    const shouldShowBanner = window.Shopify.customerPrivacy.shouldShowBanner();

    if(shouldShowBanner && visitorConsentsEmpty) {
      showBanner();
    }
  }, 2000)
}

So instead of using the setTimeout directly, you use it in your callback function and everything will work fine.

1 Like