analyticsInitUser.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import * as Amplitude from '@amplitude/analytics-browser';
  2. import * as qs from 'query-string';
  3. import {UserFixture} from 'sentry-fixture/user';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import sessionStorageWrapper from 'sentry/utils/sessionStorage';
  6. import analyticsInitUser from 'getsentry/hooks/analyticsInitUser';
  7. import trackMarketingEvent from 'getsentry/utils/trackMarketingEvent';
  8. jest.mock('getsentry/utils/trackMarketingEvent');
  9. describe('analyticsInitUser', function () {
  10. const user = UserFixture({});
  11. const _identifyInstance = new Amplitude.Identify();
  12. beforeEach(function () {
  13. sessionStorageWrapper.clear();
  14. ConfigStore.set('enableAnalytics', true);
  15. ConfigStore.set('getsentry.amplitudeApiKey', 'foo');
  16. });
  17. afterEach(function () {
  18. window.location.search = '';
  19. sessionStorageWrapper.removeItem('marketing_event_recorded');
  20. (trackMarketingEvent as jest.Mock).mockClear();
  21. (Amplitude.setUserId as jest.Mock).mockClear();
  22. (Amplitude.track as jest.Mock).mockClear();
  23. (Amplitude.Identify as jest.Mock).mockClear();
  24. });
  25. it('calls getInstance and initializes with user and user properties', function () {
  26. analyticsInitUser(user);
  27. expect(Amplitude.Identify).toHaveBeenCalledWith();
  28. expect(_identifyInstance.set).toHaveBeenCalledWith('user_id', user.id);
  29. expect(_identifyInstance.set).toHaveBeenCalledWith('isInternalUser', false);
  30. });
  31. it('calls user properties and sets isInternalUser with organization', function () {
  32. const internalUser = UserFixture({});
  33. internalUser.isSuperuser = false;
  34. internalUser.identities = [{organization: {slug: 'sentry'}}];
  35. const oldConfig = ConfigStore.getState();
  36. ConfigStore.set('user', internalUser);
  37. analyticsInitUser(internalUser);
  38. expect(Amplitude.Identify).toHaveBeenCalledWith();
  39. expect(_identifyInstance.set).toHaveBeenCalledWith('user_id', user.id);
  40. expect(_identifyInstance.set).toHaveBeenCalledWith('isInternalUser', true);
  41. ConfigStore.set('user', oldConfig.user);
  42. });
  43. it('calls user properties and sets isInternalUser with email', function () {
  44. const internalUser = UserFixture({});
  45. internalUser.isSuperuser = false;
  46. internalUser.identities = [];
  47. internalUser.emails = [{email: 'you@sentry.io', is_verified: true, id: '273025'}];
  48. const oldConfig = ConfigStore.getState();
  49. ConfigStore.set('user', internalUser);
  50. analyticsInitUser(internalUser);
  51. expect(Amplitude.Identify).toHaveBeenCalledWith();
  52. expect(_identifyInstance.set).toHaveBeenCalledWith('user_id', user.id);
  53. expect(_identifyInstance.set).toHaveBeenCalledWith('isInternalUser', true);
  54. ConfigStore.set('user', oldConfig.user);
  55. });
  56. it('handles frontend_events as array', function () {
  57. const events = [
  58. {event_name: 'Sign Up', event_label: 'Google'},
  59. {event_name: 'Start Trial'},
  60. ];
  61. const jsonEvents = JSON.stringify(events);
  62. window.location.search = qs.stringify({frontend_events: jsonEvents});
  63. analyticsInitUser(user);
  64. expect(trackMarketingEvent).toHaveBeenCalledWith('Sign Up', {event_label: 'Google'});
  65. expect(trackMarketingEvent).toHaveBeenCalledWith('Start Trial', {
  66. event_label: undefined,
  67. });
  68. expect(sessionStorageWrapper.getItem('marketing_event_recorded')).toEqual(jsonEvents);
  69. });
  70. it('handles frontend_events as single event', function () {
  71. const events = {event_name: 'Sign Up', event_label: 'Google'};
  72. const jsonEvents = JSON.stringify(events);
  73. window.location.search = qs.stringify({frontend_events: jsonEvents});
  74. analyticsInitUser(user);
  75. expect(trackMarketingEvent).toHaveBeenCalledWith('Sign Up', {event_label: 'Google'});
  76. expect(sessionStorageWrapper.getItem('marketing_event_recorded')).toEqual(jsonEvents);
  77. });
  78. it('skip sending event if in session storage', function () {
  79. const events = {event_name: 'Sign Up', event_label: 'Google'};
  80. const jsonEvents = JSON.stringify(events);
  81. window.location.search = qs.stringify({frontend_events: jsonEvents});
  82. sessionStorageWrapper.setItem('marketing_event_recorded', jsonEvents);
  83. analyticsInitUser(user);
  84. expect(trackMarketingEvent).not.toHaveBeenCalled();
  85. });
  86. it('handles malformed event_name', function () {
  87. const events = {event_name: undefined, event_label: 'Google'};
  88. const jsonEvents = JSON.stringify(events);
  89. window.location.search = qs.stringify({frontend_events: jsonEvents});
  90. analyticsInitUser(user);
  91. expect(trackMarketingEvent).not.toHaveBeenCalled();
  92. });
  93. it('store previous_referrer in local storage', function () {
  94. window.location.search = qs.stringify({referrer: 'something'});
  95. // We need to spy on the prototype since this may not be a mocked class
  96. // https://stackoverflow.com/questions/32911630/how-do-i-deal-with-localstorage-in-jest-tests
  97. const spy = jest.spyOn(Storage.prototype, 'setItem');
  98. analyticsInitUser(user);
  99. expect(spy).toHaveBeenCalledWith('previous_referrer', 'something');
  100. });
  101. it('analytics disabled', function () {
  102. ConfigStore.set('enableAnalytics', false);
  103. analyticsInitUser(user);
  104. expect(Amplitude.Identify).not.toHaveBeenCalledWith();
  105. });
  106. });