浏览器监控接入
在
nginx.conf
中添加/ta
请求处理,以返回 TA.js 的配置。环境变量在 Erda 中将默认注入。```bash
set $taEnabled ${TERMINUS_TA_ENABLE};
set $taUrl ${TERMINUS_TA_URL};
set $collectorUrl ${TERMINUS_TA_COLLECTOR_URL};
set $terminusKey ${TERMINUS_KEY};
location /ta {
default_type application/javascript;
return 200 'window._taConfig={enabled:$taEnabled,ak:"$terminusKey",url:"$taUrl",collectorUrl:"$collectorUrl"}';
}
-
if (typeof window.$ta !== 'undefined') window.$ta('setUser', [userId])
添加
Config
类读取 TA.js 环境变量,环境变量在 Erda 中将默认注入。添加
Controller
返回 TA.js 的配置@Slf4j
@RestController
@RequestMapping("/api/ta")
private TaConfig config;
@RequestMapping(produces = "application/javascript")
@ResponseBody
public String ta() {
return String.format("window._taConfig={enabled:%b,ak:\"%s\",url:\"%s\",collectorUrl:\"%s\"}", config.isTerminusTaEnable(), config.getTerminusKey(), config.getTerminusTaURL(), config.getTerminusTaCollectorURL());
}
}
-
<head>
<script src="/api/ta"></script>
<script>
var _taConfig = window._taConfig;
if (_taConfig && _taConfig.enabled) {
!function (e, n, r, t, a, o, c) {
e[a] = e[a] || function () {
(e[a].q = e[a].q || []).push(arguments)
}, e.onerror = function (n, r, t, o, c) {
e[a]("sendError", n)
}, !0), o = n.createElement(r), c = n.getElementsByTagName(r)[0], o.async = 1, o.src = t, c.parentNode.insertBefore(o, c)
}(window, document, "script", _taConfig.url, "$ta");
$ta('start', {udata: {uid: 0}, ak: _taConfig.ak, url: _taConfig.collectorUrl});
}
</script>
</head>
添加
terminus-key.js
中间件。在
layout.hbs
的head
中添加 TA.js Script。<head>
<script>
{{#if _TA_ENABLE_}}
!function(e,t,n,s,a,c,i){e[a]=e[a]||function(){(e[a].q=e[a].q||[]).push(arguments)},c=t.createElement(n),i=t.getElementsByTagName(n)[0],c.async=1,c.src=s,i.parentNode.insertBefore(c,i)}(window,document,"script","{{_TA_URL_}}","$ta");
$ta('start',{ udata: { uid: {{#if _USER_}}{{_USER_.id}}{{else}}0{{/if}} }{{#if _TERMINUS_KEY_}}, ak: '{{_TERMINUS_KEY_}}'{{/if}} , url: '{{_TA_COLLECT_URL_}}' })
{{/if}}
</script>
</head>
-