Get customer email or id in script tag

Solved
Highlighted
Tourist
38 0 1

Hello there,

I want to retrieve shopify store's logged in customer email or id in a script tag. I tried with:

window.onload = function() {
  var customerId = __st.cid;
  console.log("customerId: "+customerId);
};

and it didn't console printed anything.

And I tried with the following code:

var customerId = {{customer.id}};
console.log("customerId: "+customerId);

But this gave console error for }
What is the correct way to get the customer id/email?

0 Likes

Success.

Shopify Staff
Shopify Staff
349 48 55

Hey @user072319,

 

ScriptTags can't read these variables. You'll need to pass them to your script from a theme file.

 

E.g. theme.liquid:

 

 

<script>
someScript('{{ customer.id }}')
</script>

 

0 Likes