Analytics.js 2.2 KB

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