Shopify:获取当前使用JavaScript登录的用户ID

时间:2022-11-09 23:23:02

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.

我正在尝试为shopify构建一个应用程序,如果我可以使用javascript api或类似的东西获取当前记录的用户ID,我感兴趣。我正在查看ajax api:http://docs.shopify.com/support/your-website/themes/can-i-use-ajax-api我可以看到你可以获得当前用户拥有的当前产品添加到他的购物车,但没有关于用户ID。

Is it possible, or am I missing something?

有可能,还是我错过了什么?

5 个解决方案

#1


2  

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.

您最好的选择是创建一个在商店中安装简单ScriptTag的应用程序。然后,该脚本可以安全地将客户ID报告给应用程序。这很简单。忘记前端API,因为没有安全的方法来使用它来调用您的应用程序。

#2


7  

You can try __st.cid in JavaScript for getting customer id.

您可以在JavaScript中尝试使用__st.cid来获取客户ID。

#3


6  

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:

我发现__st变量不可靠(__st_uniqToken为一个)。我相信在Javascript中执行此操作的正确方法是使用以下调用:

ShopifyAnalytics.lib.user().id()

ShopifyAnalytics.lib.user()。ID()

You can also get the unique user id (non-logged in id) with:

您还可以使用以下命令获取唯一用户ID(未登录ID):

ShopifyAnalytics.lib.user().properties().uniqToken

ShopifyAnalytics.lib.user()。属性()。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.

这也是在结帐页面上获取它的唯一可靠方法。我之前使用过__st_uniqToken,但它已停止工作了。

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'.

注意这不再是100%万无一失。 Shopify已经改变了他们的网站在登陆和感谢页面上的工作方式。我不得不求助于以下函数来“可靠地”获取用户ID。

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.

为Shopify开发是一场真正的噩梦。我花费50%的时间每隔几周就不会破坏我的产品,因为它们。

#4


2  

In layout.liquid file you can add this code to define global customerId variable

在layout.liquid文件中,您可以添加此代码以定义全局customerId变量

{% if customer %}
  <script type="text/javascript">
   window.customerId = "{{ customer.id }}";   
  </script>
{% endif %}

#5


2  

Just get it from ShopifyAnalytics.meta.page.customerId is your script

从ShopifyAnalytics.meta.page.customerId获取它是你的脚本

#1


2  

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.

您最好的选择是创建一个在商店中安装简单ScriptTag的应用程序。然后,该脚本可以安全地将客户ID报告给应用程序。这很简单。忘记前端API,因为没有安全的方法来使用它来调用您的应用程序。

#2


7  

You can try __st.cid in JavaScript for getting customer id.

您可以在JavaScript中尝试使用__st.cid来获取客户ID。

#3


6  

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:

我发现__st变量不可靠(__st_uniqToken为一个)。我相信在Javascript中执行此操作的正确方法是使用以下调用:

ShopifyAnalytics.lib.user().id()

ShopifyAnalytics.lib.user()。ID()

You can also get the unique user id (non-logged in id) with:

您还可以使用以下命令获取唯一用户ID(未登录ID):

ShopifyAnalytics.lib.user().properties().uniqToken

ShopifyAnalytics.lib.user()。属性()。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.

这也是在结帐页面上获取它的唯一可靠方法。我之前使用过__st_uniqToken,但它已停止工作了。

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'.

注意这不再是100%万无一失。 Shopify已经改变了他们的网站在登陆和感谢页面上的工作方式。我不得不求助于以下函数来“可靠地”获取用户ID。

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.

为Shopify开发是一场真正的噩梦。我花费50%的时间每隔几周就不会破坏我的产品,因为它们。

#4


2  

In layout.liquid file you can add this code to define global customerId variable

在layout.liquid文件中,您可以添加此代码以定义全局customerId变量

{% if customer %}
  <script type="text/javascript">
   window.customerId = "{{ customer.id }}";   
  </script>
{% endif %}

#5


2  

Just get it from ShopifyAnalytics.meta.page.customerId is your script

从ShopifyAnalytics.meta.page.customerId获取它是你的脚本