Analytics.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.track = track;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. function _interopRequireDefault(e) {
  8. return e && e.__esModule ? e : {
  9. default: e
  10. };
  11. }
  12. /**
  13. * Parse.Analytics provides an interface to Parse's logging and analytics
  14. * backend.
  15. *
  16. * @class Parse.Analytics
  17. * @static
  18. * @hideconstructor
  19. */
  20. /**
  21. * Tracks the occurrence of a custom event with additional dimensions.
  22. * Parse will store a data point at the time of invocation with the given
  23. * event name.
  24. *
  25. * Dimensions will allow segmentation of the occurrences of this custom
  26. * event. Keys and values should be {@code String}s, and will throw
  27. * otherwise.
  28. *
  29. * To track a user signup along with additional metadata, consider the
  30. * following:
  31. * <pre>
  32. * var dimensions = {
  33. * gender: 'm',
  34. * source: 'web',
  35. * dayType: 'weekend'
  36. * };
  37. * Parse.Analytics.track('signup', dimensions);
  38. * </pre>
  39. *
  40. * There is a default limit of 8 dimensions per event tracked.
  41. *
  42. * @function track
  43. * @name Parse.Analytics.track
  44. * @param {string} name The name of the custom event to report to Parse as
  45. * having happened.
  46. * @param {object} dimensions The dictionary of information by which to
  47. * segment this event.
  48. * @returns {Promise} A promise that is resolved when the round-trip
  49. * to the server completes.
  50. */
  51. function track(name, dimensions) {
  52. name = name || '';
  53. name = name.replace(/^\s*/, '');
  54. name = name.replace(/\s*$/, '');
  55. if (name.length === 0) {
  56. throw new TypeError('A name for the custom event must be provided');
  57. }
  58. for (const key in dimensions) {
  59. if (typeof key !== 'string' || typeof dimensions[key] !== 'string') {
  60. throw new TypeError('track() dimensions expects keys and values of type "string".');
  61. }
  62. }
  63. return _CoreManager.default.getAnalyticsController().track(name, dimensions);
  64. }
  65. const DefaultController = {
  66. track(name, dimensions) {
  67. const RESTController = _CoreManager.default.getRESTController();
  68. return RESTController.request('POST', 'events/' + name, {
  69. dimensions
  70. });
  71. }
  72. };
  73. _CoreManager.default.setAnalyticsController(DefaultController);