Set Shopify user ID in push notification platform

If you are using Shopify and want your app users to receive push notifications for order and shipping updates, you will want to assign a common identifier to your users in your prefered push notifications platform.

To do this, we can use the Shopify user IDs, and a simple code snippet.

First, in your Shopify Dashboard go to "Online Store > Themes" then click the three dots and click "Edit Code", as you can see below:

Select the "theme.liquid" file on the left sidebar, and drop the code for your selected push platform right before the closing "</head>" tag:

Klaviyo

<script>
  // Function to check if user agent contains "canvas"
  function mlCheckUserAgentForCanvas() {
    const userAgent = navigator.userAgent.toLowerCase();
    return userAgent.includes('canvas');
  }

  // If the user agent contains "canvas"
  if (mlCheckUserAgentForCanvas()) {
    {% if customer %}
      // If the user is logged in, set their Shopify user ID
      nativeFunctions.klaviyoSetExternalUserId("{{ customer.id }}");
    {% else %}
      // If the user is not logged in, set an empty value
      nativeFunctions.klaviyoSetExternalUserId('');
    {% endif %}
  }
</script>

OneSignal

<script>
  // Function to check if user agent contains "canvas"
  function mlCheckUserAgentForCanvas() {
    const userAgent = navigator.userAgent.toLowerCase();
    return userAgent.includes('canvas');
  }

  // If the user agent contains "canvas"
  if (mlCheckUserAgentForCanvas()) {
    {% if customer %}
      // If the user is logged in, set their Shopify user ID
      nativeFunctions.onesignalSetExternalUserId("{{ customer.id }}");
    {% else %}
      // If the user is not logged in, set an empty value
      nativeFunctions.onesignalSetExternalUserId('');
    {% endif %}
  }
</script>
Thank you! We'll be in touch within 48 hours :)
Oops! Something went wrong while submitting the form.