腾讯有数官方文档

setContext(context: object)

Warning
由于context 的多层级结构修改容易出错,不再推荐直接使用该方法操作上下文,可以使用 setUsersetChan 代替。

设置通用的上下文数据内容。上报行为时总是会从上下文数据中抽取需要的数据(例如wx_user和chan)再上报,如此可以简化上报内容

接口可多次调用,新的一级属性会直接覆盖旧的属性(浅层覆盖)

注意:应该尽量在`startReport()`之前调用设置好上下文数据

属性 类型 必填 说明

wx_user

wx_user_relation

微信用户关联数据,对应公共属性wx_user

chan

chan

渠道数据,对应公共属性chan

let app = getApp()

App({
    onShow(options) {
        app.sr.setContext({
            wx_user: { // 首次添加
                user_id: 'aaa'
            }
        })
        /*
            context: {
                wx_user: {
                    user_id: 'aaa'
                }
            }
        */

        app.sr.setContext({
            wx_user: { // 更新
                user_id: 'bbb',
                open_id: 'xxx',
            },
            chan: { // 首次添加
                chan_wxapp_scene: options.scene
            }
        })
        /*
            context: {
                wx_user: {
                    user_id: 'bbb',
                    open_id: 'xxx',
                },
                chan: {
                    chan_wxapp_scene: options.scene
                }
            }
        */
    }
})