4

js を介してカスタムの key.io イベントを送信する基本的な例を設定しようとしています。現時点では、プレゼンテーションや視覚化などは必要ありません。

これは、オンラインで見つけた別の例から作成した例です。いくつかのバリエーションを試しましたが、それらはすべて Google Chrome で動作しますが、Firefox では動作しません (Ubuntu canonical の場合は 38.0 - 1.0)。

  1. マニュアルで提案されているように、インライン スクリプト ( !function(a,b){a("Keen"... ) をヘッドに追加すると、FF でエラーは発生しませんが、addEventは決して発生しないようです。が呼び出され、「err」も「res」も応答しません。

  2. CDN ( d26b395fwzu5fz.cloudfront.net/3.2.4/keen.min.js )からライブラリを含めると、ページの読み込み時にエラーが発生します。

    ReferenceError: Keen が定義されていません
    var KeinClient = new Keen({

  3. js ファイルをダウンロードしてローカルで提供すると、ボタンをクリックした後に次のエラー応答が返されます。

    エラー: リクエストが失敗しました
    err = new Error(is_err ? res.body.message : '不明なエラーが発生しました');

これらの試行はすべて Chrome から機能しますが、他のブラウザーからも機能する必要があります。

4

1 に答える 1

3

ken.io チームから返信がありました。Adblock Plus がスクリプトに干渉していることが判明しました。無効にした後、すべてが Chrome のように FF で機能します。


いくつかの調査の結果、 http ://api.keen.ioへのリクエストは、次のフィルター ルールを持つ ABP の「EasyPrivacy」フィルターによってブロックされたことが判明しました。 ~keen.io

そのため、「中間」サーバー (プロキシ) にリクエストを送信することが、私が確認できる唯一の解決策のようです。


少し具体的なユースケースがあります - 静的サイトを追跡する必要があり、レール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>

通知.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
于 2015-05-21T08:56:23.050 に答える