@aics/frontend-insights
在第三方监测/测量/分析工具之上的轻型抽象API.
插件
这个库是有用的,因为第三党的服务被插入其中;没有插件,它实际上是一个noop API.
为默认导出此软件包的插件, 即 FrontendInsights class,它充当其已配置插件的信息发送器.
此库定义了必须执行的接口,以使每个第三方服务能够用作插件 FrontendInsights。 。 。 .
界面有意尽可能泛化。 该接口是:
interface FrontendInsightsPlugin {
process(message: FrontendInsightsMessage, context: FrontendInsightsContext): void;
}
外部服务应该在插件构造器中初始化(例如给定API密钥).
之后,由插件决定是处理一些、全部还是没有由该插件发送的信件 FrontendInsights 课.
例如,一个插件完全通过对浏览器的按键程序工作 window.onerror 事件(正如一些基于浏览器的例外监测服务所做的那样),
可能选择不处理任何信件.
另一方面,分析服务可能选择处理 FrontendInsightsMessageType.UserEvent 财务报告和审定财务报表 FrontendInsightsMessageType.UserTiming 留言.
可以根据需要添加额外的插件和信件类型,而无需考虑库的现有用法.
如果有新信件类型( N)FrontendInsightsMessageType加上,
建议在《公约》中增加一种具体的公开方法 FrontendInsights 发送类似 FrontendInsights::dispatchUserEvent。 。 。 .
使用量
import FrontendInsights, { FrontendInsightsContext } from "@aics/frontend-insights";
import AmplitudeNodePlugin from "@aics/frontend-insights-plugin-amplitude-node";
const context: FrontendInsightsContext = {
application: {
name: APP_ID,
version: "4.2.2",
},
userInfo: {
userId: "[email protected]",
},
session: {
platform: "Electron"
}
};
const frontendInsights = new FrontendInsights(context, [
new AmplitudeNodePlugin({ apiKey: "abc123" }),
new WhateverPlugin({ ... }), // e.g., could be others...
]);
frontendInsights.dispatchUserEvent({
type: "ANY_STRING",
properties: {
foo: true,
bar: "no",
baz: 123,
},
});