You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在FCM中追踪Web应用推送通知的成功发送数量?

FCM推送通知发送/接收数量追踪方案

一、统计Web应用发送消息的成功次数

当你的Web应用调用FCM的sendToToken、sendToTopic等发送API时,可在请求成功回调中直接记录事件到Firebase Analytics:

  1. 确保前端已正确初始化Firebase Analytics
  2. 在发送消息的代码中添加成功/失败回调的事件日志:
    const messaging = firebase.messaging();
    const analytics = firebase.analytics();
    
    // 示例:向单个设备发送消息
    async function sendNotificationToToken(token, payload) {
      try {
        const response = await messaging.sendToToken(token, payload);
        // 发送成功,记录事件
        analytics.logEvent('fcm_message_sent_success', {
          target_token: token.substring(0, 10) + '...', // 脱敏处理
          message_type: payload.notification ? 'notification' : 'data'
        });
        console.log('消息发送成功:', response);
      } catch (error) {
        // 发送失败,记录错误事件
        analytics.logEvent('fcm_message_sent_failure', {
          error_code: error.code,
          target_token: token.substring(0, 10) + '...'
        });
        console.error('消息发送失败:', error);
      }
    }
    
  3. 后续在Firebase控制台的Analytics模块,就能看到fcm_message_sent_success事件的统计数据,包括触发次数、参数分布等。

二、统计设备成功接收通知的次数

如果需要统计设备实际收到推送的数量,需在Service Worker中监听通知相关事件,并记录Analytics事件:

  1. 在Service Worker中导入并初始化兼容SW版本的Firebase SDK:
    importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-app-compat.js');
    importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-analytics-compat.js');
    
    const firebaseConfig = {
      // 你的Firebase项目配置
    };
    
    firebase.initializeApp(firebaseConfig);
    const analytics = firebase.analytics();
    
  2. 监听通知接收和点击事件:
    // 后台消息接收(无前台页面时触发)
    self.addEventListener('message', (event) => {
      if (event.data?.notification) {
        // 记录通知送达事件
        analytics.logEvent('fcm_notification_delivered', {
          title: event.data.notification.title
        });
      }
    });
    
    // 通知点击事件
    self.addEventListener('notificationclick', (event) => {
      event.notification.close();
      // 记录通知点击事件
      analytics.logEvent('fcm_notification_clicked', {
        title: event.notification.title
      });
      // 打开应用页面(可选)
      event.waitUntil(clients.openWindow('/'));
    });
    
  3. 若Web应用在前台,通知会通过messaging.onMessage回调接收,这里也需添加事件日志:
    messaging.onMessage((payload) => {
      console.log('前台收到消息:', payload);
      analytics.logEvent('fcm_notification_received_foreground', {
        title: payload.notification.title
      });
      // 可选:显示自定义通知
      const notificationTitle = payload.notification.title;
      const notificationOptions = {
        body: payload.notification.body
      };
      return self.registration.showNotification(notificationTitle, notificationOptions);
    });
    

三、排查之前方法失效的原因

  • 直接从Service Worker调用Google Analytics的collect接口失败:大概率是请求跨域限制或payload参数格式错误(GA的collect接口要求严格的参数格式,如必须包含tid、cid、t等),使用Firebase Analytics SDK更可靠。
  • 调用logEvent无数据:检查是否正确初始化Firebase Analytics、是否在控制台启用了Analytics服务,另外事件数据存在延迟(通常几分钟到半小时),不要立即查看结果。

内容的提问来源于stack exchange,提问作者user3350093

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:40:33