# 微信小程序快速使用

> ### *在使用前，请先阅读*[*数据模型*](/shence/technical_guide/data_import/data_model.md)*的介绍。*
>
> ### *更多参数接口信息介绍可前往* [*微信小程序 SDK 使用说明*](/shence/technical_guide/detailed_guide/mp_sdk_summary/mp_sdk.md)*。*
>
> ### *如果微信小程序中使用了插件，需要使用 is\_plugin 参数，参考文档：*[*微信小程序 SDK 插件版使用说明*](/shence/technical_guide/detailed_guide/mp_sdk_summary/mp_sdk_plugin.md)*。*

[1、事件设计表（示例）](https://github.com/hxxiaolong/wendang/tree/32bdd85c96b118df803b5923753b4f1908489069/fast_access_mp.html#1-事件设计表（示例）)

[2、引入 SDK](https://github.com/hxxiaolong/wendang/tree/32bdd85c96b118df803b5923753b4f1908489069/fast_access_mp.html#2-引入%20SDK)

[3、自定义事件追踪](https://github.com/hxxiaolong/wendang/tree/32bdd85c96b118df803b5923753b4f1908489069/fast_access_mp.html#3-自定义事件追踪)

## 1. 事件设计表（示例）

事件设计表一般是由神策分析师和你们对接的同事，针对具体业务需求一起梳理的需要做埋点的 Excel 表。

| 事件和用户属性设计                     |                        |                    |
| ----------------------------- | ---------------------- | ------------------ |
| 事件表                           | 用户表                    |                    |
| 事件名（带 $ 符号的为神策预置事件，开启全埋点自动采集） | 事件属性                   | 用户属性               |
| 小程序启动事件( $MPLaunch )          | 预置事件属性                 | 预置的用户属性（开启全埋点自动采集） |
| 小程序显示事件( $MPShow )            | 预置事件属性                 | 邮箱( email )        |
| 小程序进入后台事件( $MPHide )          | 预置事件属性                 |                    |
| 小程序页面浏览事件( $MPViewScreen )    | 预置事件属性                 |                    |
| 小程序分享事件( $MPShare )           | 预置事件属性                 |                    |
| 搜索事件( search )                | 搜索关键词( searchKeyWord ) |                    |

## 2. 引入 SDK

从 github 上下载 [微信小程序 sdk](https://github.com/sensorsdata/sa-sdk-miniprogram) ，sensorsdata.js 和 sensorsdata\_conf.js

把这两个文件放在小程序的 utils 目录下，然后在 app.js 第一行添加以下代码

```
var sensors = require('./utils/sensorsdata.js');
sensors.init();
```

现在在其他 Page 里就可以通过 getApp 来使用神策的追踪了

```
var app = getApp();
app.sensors.track(eventName, properties) // 第一个参数事件名 字符串类型，第二个参数 属性值 对象类型
```

![](/files/-LeydmabNZglRULggb6T)

下面是 sensorsdata\_conf.js 文件的参数配置

![](/files/-LeydmadsYJFXAbA3Or4)

数据接收地址 server\_url 在神策分析指定项目内获取：

![](/files/-Leydck2ctJbhNeQRsjz)

## 3. 自定义事件追踪

相关文档链接：<https://sensorsdata.cn/manual/mp_sdk.html#3-自定义事件追踪>\
SDK 初始化成功后，即可以通过 app.sensors.track(event\_name,properties) 记录事件：\
• event\_name:string，必选。表示要追踪的事件名。\
• properties:object，可选。表示这个事件的属性。

例如：埋点 “ViewProduct” 事件，事件属性有商品名称，姓名等。

```
// 追踪浏览商品事件。
var app = getApp();
app.sensors.track('ViewProduct', {
    ProductName: "MacBook Pro",
    ProductPrice: 125.55
});
```

## 4. 设置用户属性

SDK 初始化成功后，即可以通过 app.sensors.setProfile(properties) 设置用户属性，如果之前存在同名属性则覆盖：\
properties:object，必选。表示要设置的用户的属性。

```
var app = getApp();
app.sensors.setProfile({
    name: 'xxx',
    gender:'male'
});
```

## 5. 用户标识

相关文档链接：<https://www.sensorsdata.cn/manual/user_identify.html#如何准确的标识用户>

在进行任何埋点之前，都应当先确定如何标识用户。**distinct\_id** 是神策用来标识用户的一段唯一的字符串。

在小程序中，会有下面 3 种 id\
1\. 默认情况下，我们会生成一个随机数 uuid ，保存在 weixin storage 中，我们暂时称这个为 **uuid**\
2\. 用户打开小程序，我们可以获得用户的 weixin openid ，我们暂时称这个为 **openid**\
3\. 客户用户账号体系中产生或保存的真实用户 id 。我们暂时称为 **"你们服务端分配给用户具体的登录 ID"**

### 5.1 使用 openid

如果不做任何操作，小程序会使用 uuid 作为 distinct\_id ，但是这个 uuid 换了设备，或者删除小程序，会重新生成。 所以一般情况下，我们建议使用 openid 作为当前的 distinct\_id。 因为获取 openid 是一个异步的操作，但是冷启动事件等会先发生，这时候这个冷启动事件的 distinct\_id 就不对了。\
所以我们会把先发生的操作，暂存起来，等获取到 openid 后再调用 sensors.init() 才会发数据。 下面是常见的两种获取 openid 的初始化代码。

```
-------app.js  

var sensors = require('sensorsdata.min.js');
// 如果你们能获取到openid
sensors.setOpenid(openid);
sensors.init();
```

```
-------app.js  

var sensors = require('sensorsdata.min.js');
// 如果后端做了 appid 和 appsecret 的配置，以及 sensorsdata_conf 里有 appid 的配置
sensors.initWithOpenid();
```

### 5.2 匿名 ID 和用户 ID 关联

默认情况下 ，SDK 会分配一个匿名 ID 来标识游客。当用户注册成功或登录成功时调用 login 方法，传入对应的用户 ID ；匿名 ID 会与对应的用户 ID 进行关联，关联成功之后视为同一个用户。 调用时机：注册成功、登录成功 、初始化 SDK（如果能获取到用户 ID）都需要调用 login 方法传入用户 ID。

```
-------app.js  

var sensors = require('sensorsdata.min.js');
// 如果能获取到"你们服务端分配给用户具体的登录 ID"，需要做用户关联情况下
sensors.login("你们服务端分配给用户具体的登录 ID");
sensors.init();
```

## 6. 埋点示例数据校验

神策分析平台埋点管理查看数据接收是否正常。 埋点---导入实时查看---导入中的数据---开始刷新； 如果在此处没有看到采集的数据，可以到埋点---埋点管理中查看是否有报错数据

![](/files/-Leydmaf5F-PD5kQt7k7)

神策分析模块查看具体的数据。

## 7. 注意事项

1. 各端埋点时事件名、事件属性类型要保持一致。
2. 小程序中数据接收地址 server\_url 需要配置到服务器域名中，且必须为 https 协议的。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://54td.gitbook.io/shence/technical_guide/hello_world/sdk_helloword/fast_access_mp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
