Analytics.js 2.2 KB

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