B.L*_*key 5 web-worker reactjs service-worker azure-application-insights
我目前在我的渐进式Web应用程序中使用ApplicationInsights-JS。它可以在我的react组件中工作,因为我可以从相关的npm包中导入所需的内容。
但是,在服务工作者中,我只能使用importScripts导入逻辑。
我确实在他们的Github页面上找到了ApplicationInsights-JS的CDN,但是似乎要使用此库初始化应用程序见解,您需要访问窗口才能存储appinsights,而服务工作人员无法这样做。
由于CDN似乎与该特定库有关,因此我尝试使用Web片段方法,但是我无法使用window,而且不确定如何实现此解决方案。
这是建议片段的副本粘贴,用于从以下位置初始化应用程序见解对象:https : //github.com/Microsoft/ApplicationInsights-JS
importScripts('https://az416426.vo.msecnd.net/beta/ai.2.min.js');
const sdkInstance = 'appInsightsSDK';
window[sdkInstance] = 'appInsights';
const aiName = window[sdkInstance];
const aisdk =
window[aiName] ||
(function(e) {
function n(e) {
i[e] = function() {
const n = arguments;
i.queue.push(function() {
i[e](...n);
});
};
}
let i = { config: e };
i.initialize = !0;
const a = document;
const t = window;
setTimeout(function() {
const n = a.createElement('script');
(n.src = e.url || 'https://az416426.vo.msecnd.net/next/ai.2.min.js'),
a.getElementsByTagName('script')[0].parentNode.appendChild(n);
});
try {
i.cookie = a.cookie;
} catch (e) {}
(i.queue = []), (i.version = 2);
for (
const r = [
'Event',
'PageView',
'Exception',
'Trace',
'DependencyData',
'Metric',
'PageViewPerformance'
];
r.length;
)
n(`track${r.pop()}`);
n('startTrackPage'), n('stopTrackPage');
const o = `Track${r[0]}`;
if (
(n(`start${o}`),
n(`stop${o}`),
!(
!0 === e.disableExceptionTracking ||
(e.extensionConfig &&
e.extensionConfig.ApplicationInsightsAnalytics &&
!0 ===
e.extensionConfig.ApplicationInsightsAnalytics
.disableExceptionTracking)
))
) {
n(`_${(r = 'onerror')}`);
const s = t[r];
(t[r] = function(e, n, a, t, o) {
const c = s && s(e, n, a, t, o);
return (
!0 !== c &&
i[`_${r}`]({
message: e,
url: n,
lineNumber: a,
columnNumber: t,
error: o
}),
c
);
}),
(e.autoExceptionInstrumented = !0);
}
return i;
})({ instrumentationKey: 'xxx-xxx-xxx-xxx-xxx' });
(window[aiName] = aisdk),
aisdk.queue && aisdk.queue.length === 0 && aisdk.trackPageView({});
Run Code Online (Sandbox Code Playgroud)
我得到的窗口未定义,这是预料之中的,但是我不确定我还可以如何使用服务工作者提供的该库。
是否有其他人有类似的实现方式,他们使用服务工作者的ApplicationInsights成功地记录了遥测?
我意识到我把这件事复杂化了。
由于我只需要跟踪自定义事件,并且不需要 appInsights 所做的所有自动页面跟踪等,因此我最终从服务工作线程中进行了提取。
我刚刚从使用反应页面发出的请求中复制了标头和正文格式。
下面成功将遥测数据记录到我的应用程序洞察仪表板:
fetch(url, {
method: 'post',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify([
{
time: '2019-05-02T15:56:37.589Z',
iKey: 'INSTRUMENTATION_KEY',
name:
'Microsoft.ApplicationInsights.INSTRUMENTATION_KEY.Event',
tags: {
'ai.user.id': 'l6Tey',
'ai.session.id': 'TL+Ry',
'ai.device.id': 'browser',
'ai.device.type': 'Browser',
'ai.operation.id': 'HUfNE',
SampleRate: '100',
// eslint-disable-next-line no-script-url
'ai.internal.sdkVersion': 'javascript:2.0.0-rc4'
},
data: {
baseType: 'EventData',
baseData: {
ver: 2,
name: 'Testing manual event',
properties: {},
measurements: {}
}
}
}
])
})
.then(json)
.then(function(data) {
})
.catch(function(error) {
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |