Analytics.js 2.6 KB

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