Analytics.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Copyright (c) 2015-present, Parse, LLC.
  14. * All rights reserved.
  15. *
  16. * This source code is licensed under the BSD-style license found in the
  17. * LICENSE file in the root directory of this source tree. An additional grant
  18. * of patent rights can be found in the PATENTS file in the same directory.
  19. *
  20. * @flow
  21. */
  22. /**
  23. * Parse.Analytics provides an interface to Parse's logging and analytics
  24. * backend.
  25. *
  26. * @class Parse.Analytics
  27. * @static
  28. * @hideconstructor
  29. */
  30. /**
  31. * Tracks the occurrence of a custom event with additional dimensions.
  32. * Parse will store a data point at the time of invocation with the given
  33. * event name.
  34. *
  35. * Dimensions will allow segmentation of the occurrences of this custom
  36. * event. Keys and values should be {@code String}s, and will throw
  37. * otherwise.
  38. *
  39. * To track a user signup along with additional metadata, consider the
  40. * following:
  41. * <pre>
  42. * var dimensions = {
  43. * gender: 'm',
  44. * source: 'web',
  45. * dayType: 'weekend'
  46. * };
  47. * Parse.Analytics.track('signup', dimensions);
  48. * </pre>
  49. *
  50. * There is a default limit of 8 dimensions per event tracked.
  51. *
  52. * @method track
  53. * @name Parse.Analytics.track
  54. * @param {String} name The name of the custom event to report to Parse as
  55. * having happened.
  56. * @param {Object} dimensions The dictionary of information by which to
  57. * segment this event.
  58. * @return {Promise} A promise that is resolved when the round-trip
  59. * to the server completes.
  60. */
  61. function track(name
  62. /*: string*/
  63. , dimensions
  64. /*: { [key: string]: string }*/
  65. )
  66. /*: Promise*/
  67. {
  68. name = name || '';
  69. name = name.replace(/^\s*/, '');
  70. name = name.replace(/\s*$/, '');
  71. if (name.length === 0) {
  72. throw new TypeError('A name for the custom event must be provided');
  73. }
  74. for (const key in dimensions) {
  75. if (typeof key !== 'string' || typeof dimensions[key] !== 'string') {
  76. throw new TypeError('track() dimensions expects keys and values of type "string".');
  77. }
  78. }
  79. return _CoreManager.default.getAnalyticsController().track(name, dimensions);
  80. }
  81. const DefaultController = {
  82. track(name, dimensions) {
  83. const RESTController = _CoreManager.default.getRESTController();
  84. return RESTController.request('POST', 'events/' + name, {
  85. dimensions: dimensions
  86. });
  87. }
  88. };
  89. _CoreManager.default.setAnalyticsController(DefaultController);