You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// since analytics has the potential to be stateful if there are any plugins added,
79
-
// to be on the safe side, we should instantiate a new instance of analytics on every request (the cost of instantiation is low).
80
-
const analytics = () =>newAnalytics({
81
-
flushAt: 1,
78
+
// Preferable to create a new analytics instance per-invocation. Otherwise, we may get a warning about overlapping flush calls. Also, custom plugins have the potential to be stateful, so we prevent those kind of race conditions.
79
+
const createAnalytics = () =>newAnalytics({
82
80
writeKey: '<MY_WRITE_KEY>',
83
-
})
84
-
.on('error', console.error);
81
+
}).on('error', console.error);
85
82
86
83
module.exports.handler=async (event) => {
87
-
...
88
-
// we need to await before returning, otherwise the lambda will exit before sending the request.
89
-
awaitnewPromise((resolve) =>
90
-
analytics().track({ ... }, resolve)
91
-
)
84
+
const analytics =createAnalytics()
85
+
86
+
analytics.identify({ ... })
87
+
analytics.track({ ... })
88
+
89
+
// ensure analytics events get sent before program exits
0 commit comments