如何使用localStorage存储用户ID [英] How to use localStorage to store user id

查看:117
本文介绍了如何使用localStorage存储用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用localStorage来存储用户ID.然后获取用户ID(如果存在),如果不存在,则使用Google的analytics.js创建并存储.

I want to use localStorage to store user id. Then fetch the user id if it exists and if it does not exist then create it using the google's analytics.js and store it.

要获取clientId,我正在使用以下代码:-

To get clientId I am using the following code:-

ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
  var clientId = tracker.get('clientId');
  console.log(tracker.get('clientId'));
});

此处生成的clientId应该存储为用户ID.

The clientId generated here should be stored as an user id.

推荐答案

设置您的ClientID,

to set your ClientID,

localStorage.setItem("clientId","someID")

获得

if (localStorage.hasOwnProperty("clientId")) {
    cli_id = localStorage.getItem("clientId")
}
else {
    cli_id = localStorage.setItem("clientId","someID") // here someid from your google analytics fetch
}
console.log(cli_id)

这篇关于如何使用localStorage存储用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆