Analytics.js 2.3 KB

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