How to get the customer_id from a ScriptTag

charles-thayer
New Member
2 0 1

Hi, We're working on an app that needs to connect our service with stores and match-up shopify customers with our user accounts. We authenticate fine, and can query various APIs, however we're unclear on how our Javascript scriptTag can determine the shopify customer id, in order to pass that to our service. We're also unclear how we might detect when the user transitions from being anonymous to logged into the shopify store.

 

Many thanks,

/charles

PS. Previous posts on the subject seem to be out of date

Alex
Shopify Staff
Shopify Staff
1555 81 306

You could try using theme code to capture the customer ID with liquid, storing it in a variable for use by your script tag:

 

// Theme code
<script>
  var customerId = {{ customer.id }};
</script>

// ScriptTag
console.log(customerId);

Cheers.

0 Likes
BirdChime
Tourist
10 0 0

@Alex is there a better alternative to this in 2021?

0 Likes
dylanpierce
Shopify Partner
140 2 48

There has to be a better way of doing this than echo'ing from a liquid template.

Some apps rely on context of the currently logged in customer. By asking merchants to update their liquid code for core functionality isn't a good answer.

Founder of Verdict - Anti-Fraud Apps for Shopify
  • Blockade - Easily block countries, IP addresses, VPNs
  • Real ID - Verify your customer's real IDs easily & securely
0 Likes