You need to enable JavaScript to run this app.
导航
Taro框架 - 多端小程序弹窗接入
最近更新时间:2025.07.29 17:51:28首次发布时间:2025.07.29 17:51:28
复制全文
我的收藏
有用
有用
无用
无用

❗️注意:该文档支持的 Taro 版本为 3.x,其他版本支持可联系对接产品

1、流程简介

Image

2、在 GMP 增加多端小程序应用

打开 GMP 系统,进入**「管理中心」** -> 「通道管理」 -> 「多端小程序弹窗」 -> 「应用管理」
Image

点击**「新建应用」**,填写 「应用名称」,然后点击 「保存」
Image

3、集成弹窗 SDK

3.1 接入 Finder SDK(可选)

❗️注意:此步骤可选,如果有在用的 UBA ,不使用 Finder ,则可以不接入
Taro 项目根目录下安装 @datarangers/sdk-mp

npm install @datarangers/sdk-mp

3.2 接入弹窗 SDK

1、在 Taro 项目根目录下安装 @byte-gmp/gmp-mutil-mini-popup-taro3

npm install @byte-gmp/gmp-mutil-mini-popup-taro3

2、在代码 /src/app.ts 里引入

import { PropsWithChildren } from 'react';
import Taro, { Current, useLaunch } from '@tarojs/taro';

// 如果 3.1 步骤有接入Finder SDK,则需要下面引入
import $$Rangers from '@datarangers/sdk-mp';
import GmpSdk from '@byte-gmp/gmp-mutil-mini-popup-taro3';
import './app.less';
import { platform } from './utils';

// 如果 3.1 步骤有接入Finder SDK,则需要下面 init 方法
// 参数配置参考 3.4.1
$$Rangers.init({
  app_id: 10000026, // 注意类型是number而非字符串,请替换成自己的Finder的app_id
  channel_domain: 'https://sdk168-238-42gmp.datarangers-onpremise.volces.com', // 请替换成自己的Finder的上报域名
  log: false, // 是否开启日志打印
  auto_report: true, // 是否开启预置事件采集
  enable_ab_test: false,
});
const gmp = GmpSdk.init({
  finderRangers: $$Rangers,
  mainAccountId: -1, // 请替换成自己的GMP的mainAccountId
  wechatAppId: 'wxbee437cfe27aec05', // 请替换成自己的wechatAppId
  appId: 1, // 请替换成自己的GMP的appId
  idType: 'app_id+open_id',
  operatingMode: 'Auto',
  popupAppId: '1748182358462304256', // 请替换成自己的GMP的popupAppId
  initPage: 'pages/index/index',
  origin: 'https://e168-238-42gmp.datarangers-onpremise.volces.com/gmpi/gmp', // 请替换成自己的GMP的url
  finderAppId: 10000026, // 注意类型是number而非字符串,请替换成自己的Finder的app_id
  customReportEvent(key, params) {
    console.log(key, 'customEventTrigger', params);
  },
  success(res) {
    console.log(res, 'success');
  },
  fail(res) {
    console.log(res, 'fail');
  },
  error(e) {
    console.log(e, 'e');
  },
});

// 如果设置完毕,就可以调用send方法标记可以上报事件了。
// 在SDK的初始化流程中,send方法必须被调用执行过,否则所有事件都不会上报
$$Rangers.send();

function App({ children }: PropsWithChildren<any>) {
  useLaunch(() => {
    $$Rangers.config({
      user_unique_id: '16', //可以是open_id,也可以union_id等其他业务觉的可以用来标识用户唯一性的值
      user_unique_id_type: 'baseid',
    });

    if (Current.app) {
    // 将 弹窗SDK 实例也挂载到全局
    // 这里命名必须与视图 SDK 组件传入的 key 保持一致,如:
    // <gmp-popup gmpEventKey="gmpEventTrigger" />,则这里属性名为 gmpEventTrigger
      (Current.app as any).gmp = gmp;
      
      // 将 finder 实例挂载到全局
      (Current.app as any).$$Rangers = $$Rangers;
      (Current.app as any).initd = false;
      (Current.app as any).reportPopupKey = '';
    }
    
    // 参数配置见 3.4.3
    GmpSdk.config({
      id: '16',
      idType: 'baseid',
      platform,
    });

    // 参数配置见 3.4.4
    GmpSdk.on((key: string) => {
      (Current.app as any).reportPopupKey = key;
      console.log('reportKey:', key);
    });

    console.log('App launched.');
  });

  // children 是将要会渲染的页面
  return children;
}

export default App;

3.3 引入弹窗组件

以首页 pages/index/index 弹窗为例

pages/index/index.tsx 文件中引入
❗️注意:在 3.2 中挂载的代码片段 (Current.app as any).gmp = gmp,属性名 为 gmp,刚好对应下面传入 <gmp-popup> gmpEventKey

import React, { Component } from 'react';
import { View, Block } from '@tarojs/components';
import { Current } from '@tarojs/taro';
import Popup from '@byte-gmp/gmp-mutil-mini-popup-taro3/dist/components/Popup';
import './index.less';

export default class Index extends Component {
  render() {
    return (
      <Block>
        <View className="index"></View>
        <Popup gmpEventKey="gmp" />
      </Block>
    );
  }
}

3.4 API

3.4.1 $$Rangers.init 参数

参数名

参数类型

必填

说明

app_id

number

finderAppId,业务产品的唯一标识,获取方式可见 3.4.5

channel_domain

string

finder 上报域名,获取方式可见3.4.5

log

boolean

设置true后,控制台会打印调试信息

auto_report

boolean

自动上报,设置true后,会自动上报预定义事件,如app_launch、app_terminate、predefine_pageview、on_share等事件

enable_ab_test

boolean

设置true后,会开启ab实验功能,包括使用getVar、getAllVars等api

3.4.2 GmpSdk.init 参数

参数名

参数类型

必填

说明

finderRangers

-

上述 $$Rangers.init({})方法返回的 finder 实例

appId

number

GMP 的项目 ID,获取方式可见3.4.5

mainAccountId

number

租户 ID,仅在Saas情况下存在。私域部署情况下填 -1,获取方式可见3.4.5

origin

string

数据请求地址,格式如: https://${GMP系统域名}/gmp,获取方式可见3.4.5

popupAppId

string

多端小程序弹窗-应用ID,获取方式可见3.4.5

success

(res: any) => void

成功回调函数,在此需要将返回值挂载到App上,且挂载的名称需要和后续调用SDK组件时传递的 key 保持一致

idType

string

id类型,表示创建多端小程序触达任务时,使用的 id类型,通常为 app_id+open_id

webId

string

设备ID

fail

(res: Error) => void;

失败回调函数

error

(res: Error) => void;

出现错误时回调函数

operatingMode

'Auto' | 'Manual'

操作模式,默认为自动模式 Auto
手动模式 Manual 需要结合下面 reportPopupKey 使用,基于回调参数 key 手动触发弹窗 具体使用方式可参考 3.4.5

reportPopupKey

(key: string) => void

当选择手动模式后,SDK会将弹窗 Key 通过该函数传出

initPage

string

初始化弹窗时,若有事件,会希望在哪个页面弹窗(一般传首页 'pages/index/index' )

finderAppId

number

Finder appid,若客户使用findersdk则必传;若客户不使用findersdk则不传。

customReportEvent

自定义获取sdk内上报信息,当不接入finder时,利用该接口监听SDK,协助上报;客户不需要关注参数具体的含义,参数的值会由GMP进行回传,有值就传,没有的值代表为空不用传GMP会提供转发给客户的数据包含具体的事件名称、属性以及属性的值;客户将GMP提供的事件和属性数据的值按照三方UBA的标准上报给三方UBA即可

(event: string, params: {
task_id: int; //规则会带个task_id字段
task_type: string; //规则会带个task_type字段
sub_task_id: int; //服务端带在Node里,extra_params字段;如果是app_sender_event的事件,取弹窗节点的sub_task_id字段
log_id: string; //存在服务端触发节点的情况,会返回log_id。如果第一个节点是完成事件A,则由客户端生成log_id
popup_app_id: string; // 弹窗应用id
platform: string; // 平台与初始化时的一致,android ios baidu wechat zhifubao
channle_type: int; //app能取到;7-App弹窗 18-小程序弹窗 20-多端小程序弹窗
rule_id: int; //规则会带个rule_id字段
schedule_id: int; //存在服务端触发节点的情况,会返回log_id。如果第一个节点是完成事件A,就不需要回传这个字段--可以为空
current_version_index: int; //过ab分流节点后带上--可以为空
status_code:string; // 有一定的映射关系,详情看 5.3
}) => void

3.4.3 GmpSdk.config 参数

参数名

参数类型

必填

说明

id

string

用户 id
如果 id 类型为单ID,类似 baseid,那么这里直接传值
如果 id 类型为 组合ID,类似app_id+open_id,那么这里值用 | 分隔符分开,形式为 ${app_id}|${open_id},例如 wx09092|1003

idType

string

Id 类型,例如 baseid

platform

string

'wechat''baidu' 或者 'zhifubao'

3.4.4 GmpSdk.on

参数名

参数类型

必填

说明

fn

(key: string) => void

选择手动模式时(即 GmpSdk.initoperatingMode 设置为 'Manual')需要调用该方法,监听弹窗触发的时机。
弹窗即将触发时,会触发该方法,并将 弹窗Key 作为参数返回到回调函数中

3.4.5 SDK 参数获取

在 GMP 打开**「管理中心」** -> 「通道管理」 -> 「多端小程序弹窗」 -> 「SDK参数」
Image
其中,Finder app_id 的值可能有多个,触达时要用哪个应用,这里就选择应用对应后面括号里的 app_id

3.4.6 手动模式

GmpSdk.init 设置为 'Manual',则进入手动模式。手动模式在调用 GmpSdk.config之后,需要使用 GmpSdk.on 方法进行监听,如果有弹窗将要弹出时,会将 弹窗 Key 以回调函数参数的形式给出来,例如:

function App({ children }: PropsWithChildren<any>) {
  useLaunch(() => {
    $$Rangers.config({
      user_unique_id: '16', //可以是open_id,也可以union_id等其他业务觉的可以用来标识用户唯一性的值
      user_unique_id_type: 'baseid',
    });

    if (Current.app) {
    // 将 弹窗SDK 实例也挂载到全局
    // 这里命名必须与视图 SDK 组件传入的 key 保持一致,如:
    // <gmp-popup gmpEventKey="gmpEventTrigger" />,则这里属性名为 gmpEventTrigger
      (Current.app as any).gmp = gmp;
      
      // 将 finder 实例挂载到全局
      (Current.app as any).$$Rangers = $$Rangers;
      (Current.app as any).initd = false;
      (Current.app as any).reportPopupKey = '';
    }
    
    // 参数配置见 3.4.3
    GmpSdk.config({
      id: '16',
      idType: 'baseid',
      platform,
    });

    // 参数配置见 3.4.4
    GmpSdk.on((key: string) => {
      // 这里 key 可以做任意处理,比如存到全局变量中
      console.log('reportKey:', key);
          
      // 可以在这里做任何逻辑
          
      // 需要弹窗的时候调用以下方法可以触发弹窗
      (Current.app as any).gmpEventTrigger.triggerPopupData(key);
    });

    console.log('App launched.');
  });

  // children 是将要会渲染的页面
  return children;
}

3.4.7 上报 params

在手动上报时,会遇到上报 params 场景,如下:

(Current.app as any).gmpEventTrigger.eventTrigger(key, params)

params 代表触发事件时的属性集合,类型为可被 JSON.parse的对象,例如:

{
  // 停留时长
  duration: 100,
  // 当前触发页面
  path: "pages/index/index",
  // 场景值
  scene: 1001,
}

上述的 停留时长(duration),会用于与**「触达任务」**-> **「触发条件」**做比较,以此来判断是否符合触发条件:
Image
假如触达任务中没有判断属性满足条件,则该值不必传。

4、小程序后台配置

将上述 3.4.1channel_domain(Finder 上报域名),3.4.2origin (GMP 域名),配置到下面三个小程序后台里:

4.1 微信小程序管理后台

配置到「request 合法域名」里
Image

4.2 支付宝小程序管理后台

Image

4.3 百度小程序管理后台

Image

5、弹窗接入测试

5.1 创建触达任务

❗️注意:测试之前,必须先确保已经导入行为事件;测试只限在小程序本地调试、体验版进行,不能直接发布正式版
创建**「触达任务」,选择「微信小程序弹窗」「任务关联应用」(未购买 Finder 可忽略,与 3.4.1 参数 app_id 保持一致),点击「确定」**
Image

选择**「受众用户」(建议选择「全部用户」**进行测试验证)
Image

触发条件中选择任一事件,并记住这里的**「事件英文名」,如图中为 request_status__
Image
触达配置选择任一模板,点击弹窗,编辑
「点击行为」「关闭弹窗」**,然后创建触达任务并审批
Image
Image

5.2 任务弹窗测试

5.2.1 使用 Finder SDK的场景

在**「引入弹窗组件」**(即引入 <gmp-popup> 组件)的页面代码里(例如首页是 pages/index/index.tsx),加入下述代码,触发 4.1 中创建触达任务选择的事件英文名,本例子为:request_status__

componentDidMount(): void {
  (Current.app as any).$$Rangers.event('request_status__');
}

如果弹出弹窗,则代表接入测试成功,可以把上述测试代码删除,创建正式触达任务弹窗

5.2.2 自带 UBA 的场景

在 UBA 监听到事件触发时,手动触发 GMP SDK 的 eventTrigger 方法,key 为事件名,本例子为:request_status__

// UBA 监听事件触发之后插入下面代码:
// key 表示事件名,本例子中以下 key 为 request_status__
// params 指触发事件时某些属性的值,非必传,用法可参考 3.4.7
(Current.app as any).gmpEventTrigger.eventTrigger(key, params)

如果弹出弹窗,则代表接入测试成功,可以创建正式触达任务弹窗

5.3 测试完整代码

// src/app.ts
import { PropsWithChildren } from 'react';
import Taro, { Current, useLaunch } from '@tarojs/taro';
import $$Rangers from '@datarangers/sdk-mp';
import GmpSdk from '@byte-gmp/gmp-mutil-mini-popup-taro3';
import './app.less';
import { platform } from './utils';

$$Rangers.init({
  app_id: 10000026, // 注意类型是number而非字符串
  channel_domain: 'https://sdk168-238-42gmp.datarangers-onpremise.volces.com',
  log: false, // 是否开启日志打印
  auto_report: true, // 是否开启预置事件采集
  enable_ab_test: false,
});
const gmp = GmpSdk.init({
  finderRangers: $$Rangers,
  mainAccountId: -1,
  appId: 1,
  idType: 'app_id+open_id',
  operatingMode: 'Auto',
  popupAppId: '1748182358462304256',
  initPage: 'pages/index/index',
  origin: 'https://e168-238-42gmp.datarangers-onpremise.volces.com/gmpi/gmp',
  finderAppId: 10000026, // 注意类型是number而非字符串
  customReportEvent(key, params) {
    console.log(key, 'customEventTrigger', params);
  },
  success(res) {
    console.log(res, 'success');
  },
  fail(res) {
    console.log(res, 'fail');
  },
  error(e) {
    console.log(e, 'e');
  },
});

// 如果设置完毕,就可以调用send方法标记可以上报事件了。
// 在SDK的初始化流程中,send方法必须被调用执行过,否则所有事件都不会上报
$$Rangers.send();

function App({ children }: PropsWithChildren<any>) {
  useLaunch(() => {
    $$Rangers.config({
      user_unique_id: '16', //可以是open_id,也可以union_id等其他业务觉的可以用来标识用户唯一性的值
      user_unique_id_type: 'baseid',
    });

    if (Current.app) {
      (Current.app as any).gmp = gmp;
      (Current.app as any).$$Rangers = $$Rangers;
      (Current.app as any).initd = false;
      (Current.app as any).reportPopupKey = '';
    }
    GmpSdk.config({
      id: '16',
      idType: 'baseid',
      platform,
    });

    GmpSdk.on((key: string) => {
      (Current.app as any).reportPopupKey = key;
      console.log('reportKey:', key);
    });

    console.log('App launched.');
  });

  // children 是将要会渲染的页面
  return children;
}

export default App;

// pages/index/index.tsx
import React, { Component } from 'react';
import { View, Block } from '@tarojs/components';
import { Current } from '@tarojs/taro';
import Popup from '@byte-gmp/gmp-mutil-mini-popup-taro3/dist/components/Popup';
import './index.less';

export default class Index extends Component {
  componentDidMount(): void {
    (Current.app as any).$$Rangers.event('request_status__');
  }

  render() {
    return (
      <Block>
        <View className="index">GMP SDK taro多端弹窗</View>
        <Popup gmpEventKey="gmp" />
      </Block>
    );
  }
}

6、参考示例
taro3-1707196787.zip
233.85KB

7、常见问题

7.1 弹窗没有正常弹出

  1. 检查 GmpSdk.init 配置的 success 回调是否有进入
  2. 检查SDK初始化后,是否正常触发gmp/openapi/v3/popup/get_miniprogram_sdk_statusopenapi/v3/rule_engine/get_popup_rule_list接口,有无出现接口报错
  3. 检查希望出现弹窗的页面是否正常接入了弹窗组件,页面中元素是否出现gmp-popup标签,是否在触发事件后出现内容

7.2 数据分析展示异常

  1. 检查异常的数据分析的项目id和初始化时传入的项目id是否一致
  2. 检查DataFinder的SDK是否正常接入以及是否触发上报函数
  3. 检查DataFinder的上报函数是否存在异常,且在DataFinder中是否存在上报的数据