How to add keen.io base event with javascript

I am trying to set up a basic example that dispatches a keen.io custom event via js. At the moment I do not need a presentation, visualization, etc.

Here is an example that I created from another found on the Internet. I tried several options, and all of them work in Google Chrome, but none of them work in Firefox (38.0 for Ubuntu canonical - 1.0).

  • if you add the built-in script (! function (a, b) {a ("Keen" ...), as suggested in the manual, to the head, I get no errors in FF, but it seems that addEvent is never called, and it not responding, "err" and "res".

  • If I turn on the library from the CDN (d26b395fwzu5fz.cloudfront.net/3.2.4/keen.min.js), I get an error when loading the page:

    ReferenceError: Kin not defined
    var keenClient = new Keen ({

  • If I download the js file and service it locally, after clicking the button, I get the following error response:

    Error: request failed. err = new Error (is_err? res.body.message: "Unknown error");

All these attempts work from Chrome, but I need this to work from other browsers as well.

+4
source share
1 answer

I received a response from the keen.io team. It turned out that Adblock Plus is interfering with the script. After I turned it off, everything works in FF, like in Chrome.


, http://api.keen.io "EasyPrivacy" ABP : . ^ $ , = ~ keen.imtqy.com | ~ keen.io

, "" () , .


- , 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
+2

Source: https://habr.com/ru/post/1588946/


All Articles