项目作者: sematext

项目描述 :
Repository for the web browser SDK used for Sematext Experience.
高级语言: JavaScript
项目地址: git://github.com/sematext/browser-sdk.git
创建时间: 2020-09-14T10:30:42Z
项目社区:https://github.com/sematext/browser-sdk

开源协议:Apache License 2.0

下载


Sematext Browser SDK

Build Status
License

Repository containing the Browser SDK for the Sematext Experience. It is used to collect end-user data from browsers using various browser APIs. It is designed to work with traditional web applications as well as single-page apps (SPAs). The collected data includes:

Sematext Cloud Experience

Development

The Experience script is developed using ECMAScript 2015. For builds and development it uses:

The Experience Script Architecture

There are two parts of the Experience script:

  • The loader script
  • The metrics gathering script

The loader script is responsible for loading the metrics gathering script.
It adds the actual <script> tags with the content of the main metric script.
The loader script keeps track and buffers commands that will be sent once the
metrics gathering script loads and is configured.

Please note that the loader and the Experience script depend on each other.
Before changing the scripts keep in mind that developers who have already added the script
to their website will use the older loader script unless they update it.

Working on the Loader Script

The main entry point of the loader script is in src/snippet.js.
This is the unminified ES2015) version
of the loader script which is responsible for loading the metrics gathering script.

To generate the minified snippet of the loader script use yarn run generate-snippet.

Keep in mind that the loader script should be compact.

Working on the Main Experience Script

The main entry point of the Experience script is in src/index.js. Use eslint to catch any
style issues and flow to catch any type errors.

You can use the following commands:

  • yarn run start to start the development server
  • yarn run lint to run lint on the code
  • yarn run flow to run flow type checks
  • yarn run build to build the production bundle
  • yarn run dev to build the development (not minified) bundle

Manual Tests

You can test the loader and Experience scripts using one of the test websites that are provided:

  • test.html - simple app that can be used to generate page load events and HTTP requests
  • testspa.html - simple single-page app used to generate routeChange events and HTTP requests
  • large.html - app used for testing large requests sent from the Experience script
  • e2e.html - app used for most of the integration tests

Run yarn run start to run a small test website which runs the dev
version of the Experience script. After making changes to the script, reload the
website to see the changes.

If you wish to automatically test single page applications there is a second test application
called spa.html (see testspa.html file) which you start by running yarn run start. It
automatically runs routeChange commands every 10 seconds and HTTP requests every 5 seconds.

Automatic Tests

Cypress.io is used for automatic testing. The integration
tests are included in the cypress/integration/ directory and cover all the crucial
features of the script itself. The tests are using the same set of HTML files used
by the manual tests.

To run the full set of tests run yarn e2e. The results will include a table with test
results similar to the following:

  1. Spec Tests Passing Failing Pending Skipped
  2. ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  3. buffer.spec.js 00:10 1 1 - - -
  4. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  5. elementtiming.spec.js 00:46 3 3 - - -
  6. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  7. integration.spec.js 00:15 4 4 - - -
  8. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  9. large.spec.js 00:05 1 - 1 - -
  10. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  11. longtask.spec.js 00:30 2 2 - - -
  12. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  13. transactions.spec.js 00:15 1 1 - - -
  14. ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  15. web-vitals.spec.js 00:10 1 1 - - -
  16. └────────────────────────────────────────────────────────────────────────────────────────────────┘
  17. 1 of 7 failed (14%) 02:15 13 12 1 - -

Sending data to multiple receivers

In order to configure the Experience script to send data to multiple Experience receivers you
need to add the Experience snippet multiple times. Only the first snippet should have
URL to the Experience script set so that it’s loaded only once, for example:

  1. <script type="text/javascript">
  2. ...
  3. INSERT SCRIPT SNIPPET HERE
  4. ...
  5. (window,document,"script","/experience.js","strumTest");
  6. </script>
  7. <script type="text/javascript">
  8. ...
  9. INSERT SCRIPT SNIPPET HERE
  10. ...
  11. (window,document,"script",null,"strumUS");
  12. </script>
  13. <script type="text/javascript">
  14. window.STRUM_CONTEXTS = ['strumTest', 'strumUS'];
  15. var receivers = [
  16. 'https://rum-receiver.test.sematext.com',
  17. 'https://rum-receiver.sematext.com',
  18. ]
  19. strumTest('config', {
  20. token: 'b12s3ssd-d11s-2d39-7231-f3s22grd635f',
  21. receiverUrl: receivers[0],
  22. ignoreOrigins: receivers,
  23. });
  24. strumUS('config', {
  25. token: '8763d12d-1j3t-932v-b498-544290z98k43',
  26. receiverUrl: receivers[1],
  27. ignoreOrigins: receivers,
  28. });
  29. </script>

Contributing

Pull requests for bug fixes, improvements and new features are more than welcome. When opening
a new pull request please take the time to briefly describe the changes. Make sure that the newly
introduced code passes the lint and flow checks along with the integration tests. Once the PR
is submitted we will review and merge your changes.