我正在尝试设置一个通过js发送自定义keen.io事件的基本示例。目前我不需要任何演示,可视化等。
我在网上找到的另一个创建的Here is the example。我尝试了几种变体,所有这些变体都适用于谷歌浏览器,但它们都不适用于Firefox(38.0适用于Ubuntu规范 - 1.0)。
如果我在手册中添加了内联脚本(!function(a,b){a(“Keen”... ),我会这样做在FF中没有任何错误,但似乎 addEvent 永远不会被调用,它不会产生任何响应,“错误”也不会“res”。
如果我从CDN中包含该库( d26b395fwzu5fz.cloudfront.net/3.2.4/keen.min.js ),则在加载页面时出现错误:< / p>
ReferenceError:没有定义Keen var keenClient = new Keen({
如果我下载js文件并在本地提供,在单击按钮后,我收到以下错误响应:
错误:请求失败
err = new Error(is_err?res.body.message:'发生未知错误');
所有这些尝试都适用于Chrome,但我也需要在其他浏览器中使用此功能。
答案 0 :(得分:3)
我收到了keen.io团队的回复。事实证明,Adblock Plus正在干扰脚本。禁用后,所有功能都可以在FF中使用。
经过一番调查后发现,对http://api.keen.io的请求被&#34; EasyPrivacy&#34;使用以下过滤规则过滤ABP:keen.io ^ $ third-party,domain = ~keen.github.io | ~keen.io
所以,向&#34;中间发送请求&#34;服务器(代理)似乎是我能看到的唯一解决方案。
我们有一个特定的用例 - 需要跟踪静态站点以及对rails api服务器的可用访问,但我们最终使用的解决方案可能对某人有用。
<强>的error.html 强>
<html>
<head>
<title>Error</title>
<script src="/js/vendor/jquery-1.11.2.min.js"></script>
<script src="/js/notification.js"></script>
<script type="text/javascript">
$(document).on('ready', function () {
try {
$.get(document.URL).complete(function (xhr, textStatus) {
var code = xhr.status;
if (code == 200) {
var codeFromPath = window.location.pathname.split('/').reverse()[0].split('.')[0];
if (['400', '403', '404', '405', '414', '416', '500', '501', '502', '503', '504'].indexOf(codeFromPath) > -1) {
code = codeFromPath;
}
}
Notification.send(code);
});
}
catch (error) {
Notification.send('error.html', error);
}
});
</script>
</head>
<body>
There was an error. Site Administrators were notified.
</body>
</html>
<强> notification.js 强>
var Notification = (function () {
var endpoint = 'http://my-rails-server-com/notice';
function send(type, jsData) {
try {
if (jsData == undefined) {
jsData = {};
}
$.post(endpoint, clientData(type, jsData));
}
catch (error) {
}
}
// private
function clientData(type, jsData) {
return {
data: {
type: type,
jsErrorData: jsData,
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
pageXOffset: window.pageXOffset,
pageYOffset: window.pageYOffset,
status: status,
navigator: {
appCodeName: navigator.appCodeName,
appName: navigator.appName,
appVersion: navigator.appVersion,
cookieEnabled: navigator.cookieEnabled,
language: navigator.language,
onLine: navigator.onLine,
platform: navigator.platform,
product: navigator.product,
userAgent: navigator.userAgent
},
history: {
length: history.length
},
document: {
documentMode: document.documentMode,
documentURI: document.documentURI,
domain: document.domain,
referrer: document.referrer,
title: document.title,
URL: document.URL
},
screen: {
width: screen.width,
height: screen.height,
availWidth: screen.availWidth,
availHeight: screen.availHeight,
colorDepth: screen.colorDepth,
pixelDepth: screen.pixelDepth
},
location: {
hash: window.location.hash,
host: window.location.host,
hostname: window.location.hostname,
href: window.location.href,
origin: window.location.origin,
pathname: window.location.pathname,
port: window.location.port,
protocol: window.location.protocol,
search: window.location.search
}
}
}
}
return {
send: send
}
}());
从js代码手动发送通知的示例:
try {
// some code that may produce an error
}
catch (error) {
Notification.send('name of keen collection', error);
}
<强>轨强>
# gemfile
gem 'keen'
#routes
resource :notice, only: :create
#controller
class NoticesController < ApplicationController
def create
# response to Keen.publish does not include an ID of the newly added notification, so we add an identifier
# that we can use later to easily track down the exact notification on keen
data = params['data'].merge('id' => Time.now.to_i)
Keen.publish(data['type'], data) unless dev?(data)
# we send part of the payload to a company chat, channel depends on wheter the origin of exception is in dev or production
ChatNotifier.notify(data, dev?(data)) unless data['type'] == '404'
render json: nil, status: :ok
end
private
def dev?(data)
%w(site local).include?(data['location']['origin'].split('.').last)
end
end