You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

  1. const _ = require('lodash')
  2. const axios = require('axios')
  3. const bugsnag = require('bugsnag')
  4. const path = require('path')
  5. const uuid = require('uuid/v4')
  6. /* global WIKI */
  7. module.exports = {
  8. cid: '',
  9. enabled: false,
  10. init() {
  11. this.cid = uuid()
  12. bugsnag.register(WIKI.data.telemetry.BUGSNAG_ID, {
  13. appVersion: WIKI.version,
  14. autoNotify: false,
  15. hostname: this.cid,
  16. notifyReleaseStages: ['production'],
  17. packageJSON: path.join(WIKI.ROOTPATH, 'package.json'),
  18. projectRoot: WIKI.ROOTPATH,
  19. useSSL: true
  20. })
  21. bugsnag.onBeforeNotify((notification, originalError) => {
  22. if (!this.enabled) { return false }
  23. })
  24. if (_.get(WIKI.config, 'logging.telemetry', false) === true) {
  25. this.enabled = true
  26. }
  27. return this
  28. },
  29. sendError(err) {
  30. bugsnag.notify(err, { userId: this.cid })
  31. },
  32. sendEvent(eventCategory, eventAction, eventLabel) {
  33. if (!this.enabled) { return false }
  34. axios({
  35. method: 'post',
  36. url: WIKI.data.telemetry.GA_REMOTE,
  37. headers: {
  38. 'Content-type': 'application/x-www-form-urlencoded'
  39. },
  40. params: {
  41. v: 1, // API version
  42. tid: WIKI.data.telemetry.GA_ID, // Tracking ID
  43. aip: 1, // Anonymize IP
  44. ds: 'server', // Data source
  45. cid: this.cid, // Client ID
  46. t: 'event', // Hit Type
  47. ec: eventCategory, // Event Category
  48. ea: eventAction, // Event Action
  49. el: eventLabel // Event Label
  50. }
  51. }).then(resp => {
  52. if (resp.status !== 200) {
  53. WIKI.logger.warn('Unable to send analytics telemetry request.')
  54. }
  55. }, err => {
  56. WIKI.logger.warn('Unable to send analytics telemetry request.')
  57. })
  58. }
  59. }