Reputation: 10949
I'm trying to build an app for shopify and I'm interested if I can get the current logged user ID using javascript api or something similar. I was looking at the ajax api: http://docs.shopify.com/support/your-website/themes/can-i-use-ajax-api and I can see you can get the current products that the current user has added to his shopping cart, but nothing about the user ID.
Is it possible, or am I missing something?
Upvotes: 15
Views: 21060
Reputation: 672
This solution is not 100% about getting user_id
, but about detecting if current user is logged in as admin. You can use typeof Shopify.AdminBar
to check if the admin bar exists. It's automatically created by Shopify if the current user is admin. Maybe this will help someone.
Upvotes: -1
Reputation: 19443
It seems that Shopify API was again changed, I see that now the user Id is in ShopifyAnalytics.lib.user().traits().uniqToken
. Also, it may take time for this value to be loaded.
I use this code snippet to perform the job once the value available.
<script type="text/javascript">
actOnUser = function(retry, func){
if (ShopifyAnalytics && ShopifyAnalytics.lib && ShopifyAnalytics.lib.user) {
console.log(ShopifyAnalytics.lib.user().traits().uniqToken); // Do your work here.
} else {
if (retry > 0) {
setTimeout(function() {
func(retry - 1, func);
}, 1000);
}
console.log('User not ready'); // Can be removed - just for debug
}
}
console.log('Starting');
setTimeout(function() {
actOnUser(10, actOnUser);
}, 1000);
</script>
Upvotes: 4
Reputation: 109
Just get it from ShopifyAnalytics.meta.page.customerId
is your script
Upvotes: 10
Reputation: 4182
In layout.liquid file you can add this code to define global customerId variable
{% if customer %}
<script type="text/javascript">
window.customerId = "{{ customer.id }}";
</script>
{% endif %}
Upvotes: 10
Reputation: 4239
I've found the __st
variables unreliable (__st_uniqToken
for one).
I believe the proper way to do this in Javascript is using the following call:
ShopifyAnalytics.lib.user().id()
You can also get the unique user id (non-logged in id) with:
ShopifyAnalytics.lib.user().properties().uniqToken
This is also the only reliable way to get it on the checkout page. I previously used __st_uniqToken
, but it has since stopped working.
NOTE This is no longer 100% fool-proof. Shopify have AGAIN changed how their site works on the landing and thank-you pages. I've had to resort to the following function to get a user id 'reliably'.
var user_id = function() {
try {
return ShopifyAnalytics.lib.user().id();
} catch(e) {}
try {
return ShopifyAnalytics.lib.user().properties().uniqToken;
} catch(e) {}
try {
return ShopifyAnalytics.lib.user().anonymousId();
} catch(e) {}
return __st_uniqToken;
};
Developing for Shopify is a true nightmare. I spend 50% of my time un-breaking my product every few weeks because them.
Upvotes: 19
Reputation: 11427
Your best bet would be to create an App that installs a simple ScriptTag in the shop. That script can then report the customer ID back to the App securely. Very simple to do that. Forget the front-end API as there is no secure way to call your App using that.
Upvotes: 6