【问题标题】:running an analytic cloud functions locally and storing analytic event into a firestore collection?在本地运行分析云功能并将分析事件存储到 Firestore 集合中?
【发布时间】:2020-03-27 10:43:59
【问题描述】:

我有一个关于运行分析云函数的问题,只要发生任何注册然后它就会触发,并且该事件存储在一个集合中我正在使用 firebase emulators:start --only 函数运行该函数以及何时我正在运行函数正常函数运行良好,而不是这里的分析函数是我的代码? 我也想知道这是存储到 Firestore 集合中的正确方法吗?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const serviceAccountKey = require('./keyv2.json')

admin.initializeApp({
    credential: admin.credential.cert(serviceAccountKey),
});
const db = admin.firestore()
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
 console.log('hello')
});

exports.eventStore = functions.analytics.event('sign_up').onLog(async event => {
    console.log('hello')
    console.log(event)
    const user = event.user
    await db.collection('EventLogs').add(user)
    console.log('eventLog is added in the EventLogs collection')
})

【问题讨论】:

    标签: node.js google-analytics google-cloud-firestore google-cloud-functions firebase-analytics


    【解决方案1】:

    Firebase 模拟器目前未实现 Google Analytics for Firebase 事件。请参阅模拟器套件上的 Firebase 文档,特别是 which Firebase features and platforms are supported?

    部分

    您的 Analytics 触发函数的代码看起来不错,但您需要:

    1. 确保您的代码返回某些内容到 Cloud Functions 环境,以便它知道您的代码何时完成。在这种情况下,函数末尾的简单return true 就足够了。
    2. 我不完全确定 event.user 是否可以序列化为 JSON,因此可能值得将其包装在 JSON.parse(JSON.stringify(user)) 中。

    【讨论】:

    • 因为当我尝试使用您的第二点时,它会给出错误错误:参数“数据”的值不是像 JSON.parse(JSON.stringify(user) 这样的有效 Firestore 文档代码结构) 等待 db.collection('EventLogs').add(user)
    • 这很有趣,因为看起来user 对象中可能有一些数据无法序列化为 JSON。您可能想先看看是否:1) user 确实有一个值,2) 您可以编写 user 的一些简单属性而不是完整对象,例如只是 user.firstOpenTime
    猜你喜欢
    • 2019-10-06
    • 2022-01-09
    • 1970-01-01
    • 2017-03-02
    • 2021-02-08
    • 2019-05-10
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多