Analytics.js 2.7 KB

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