AnalyticsUTMTrackingMiddleware.test.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { assert, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockRequest from '../helpers/MockRequest.js'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. const MODULE_PATH = new URL(
  6. '../../../../app/src/Features/Analytics/AnalyticsUTMTrackingMiddleware',
  7. import.meta.url
  8. ).pathname
  9. describe('AnalyticsUTMTrackingMiddleware', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.analyticsId = 'ecdb935a-52f3-4f91-aebc-7a70d2ffbb55'
  12. ctx.userId = '61795fcb013504bb7b663092'
  13. ctx.req = new MockRequest()
  14. ctx.res = new MockResponse()
  15. ctx.next = sinon.stub().returns()
  16. ctx.req.session = {
  17. user: {
  18. _id: ctx.userId,
  19. analyticsId: ctx.analyticsId,
  20. },
  21. }
  22. vi.doMock(
  23. '../../../../app/src/Features/Analytics/AnalyticsManager.js',
  24. () => ({
  25. default: (ctx.AnalyticsManager = {
  26. recordEventForSession: sinon.stub().resolves(),
  27. setUserPropertyForSessionInBackground: sinon.stub(),
  28. }),
  29. })
  30. )
  31. vi.doMock('@overleaf/settings', () => ({
  32. default: {
  33. siteUrl: 'https://www.overleaf.com',
  34. },
  35. }))
  36. ctx.AnalyticsUTMTrackingMiddleware = (await import(MODULE_PATH)).default
  37. ctx.middleware = ctx.AnalyticsUTMTrackingMiddleware.recordUTMTags()
  38. })
  39. describe('without UTM tags in query', function () {
  40. beforeEach(function (ctx) {
  41. ctx.req.url = '/project'
  42. ctx.middleware(ctx.req, ctx.res, ctx.next)
  43. })
  44. it('user is not redirected', function (ctx) {
  45. assert.isFalse(ctx.res.redirected)
  46. })
  47. it('next middleware is executed', function (ctx) {
  48. sinon.assert.calledOnce(ctx.next)
  49. })
  50. it('no event or user property is recorded', function (ctx) {
  51. sinon.assert.notCalled(ctx.AnalyticsManager.recordEventForSession)
  52. sinon.assert.notCalled(
  53. ctx.AnalyticsManager.setUserPropertyForSessionInBackground
  54. )
  55. })
  56. })
  57. describe('with all UTM tags in query', function () {
  58. beforeEach(function (ctx) {
  59. ctx.req.url =
  60. '/project?utm_source=Organic&utm_medium=Facebook&utm_campaign=Some%20Campaign&utm_content=foo-bar&utm_term=overridden'
  61. ctx.req.query = {
  62. utm_source: 'Organic',
  63. utm_medium: 'Facebook',
  64. utm_campaign: 'Some Campaign',
  65. utm_content: 'foo-bar',
  66. utm_term: 'overridden',
  67. }
  68. ctx.middleware(ctx.req, ctx.res, ctx.next)
  69. })
  70. it('user is redirected', function (ctx) {
  71. assert.isTrue(ctx.res.redirected)
  72. assert.equal('/project', ctx.res.redirectedTo)
  73. })
  74. it('next middleware is not executed', function (ctx) {
  75. sinon.assert.notCalled(ctx.next)
  76. })
  77. it('page-view event is recorded for session', function (ctx) {
  78. sinon.assert.calledWith(
  79. ctx.AnalyticsManager.recordEventForSession,
  80. ctx.req.session,
  81. 'page-view',
  82. {
  83. path: '/project',
  84. utm_source: 'Organic',
  85. utm_medium: 'Facebook',
  86. utm_campaign: 'Some Campaign',
  87. utm_content: 'foo-bar',
  88. utm_term: 'overridden',
  89. }
  90. )
  91. })
  92. it('utm-tags user property is set for session', function (ctx) {
  93. sinon.assert.calledWith(
  94. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  95. ctx.req.session,
  96. 'utm-tags',
  97. 'Organic;Facebook;Some Campaign;foo-bar'
  98. )
  99. })
  100. })
  101. describe('with some UTM tags in query', function () {
  102. beforeEach(function (ctx) {
  103. ctx.req.url =
  104. '/project?utm_medium=Facebook&utm_campaign=Some%20Campaign&utm_term=foo'
  105. ctx.req.query = {
  106. utm_medium: 'Facebook',
  107. utm_campaign: 'Some Campaign',
  108. utm_term: 'foo',
  109. }
  110. ctx.middleware(ctx.req, ctx.res, ctx.next)
  111. })
  112. it('user is redirected', function (ctx) {
  113. assert.isTrue(ctx.res.redirected)
  114. assert.equal('/project', ctx.res.redirectedTo)
  115. })
  116. it('next middleware is not executed', function (ctx) {
  117. sinon.assert.notCalled(ctx.next)
  118. })
  119. it('page-view event is recorded for session', function (ctx) {
  120. sinon.assert.calledWith(
  121. ctx.AnalyticsManager.recordEventForSession,
  122. ctx.req.session,
  123. 'page-view',
  124. {
  125. path: '/project',
  126. utm_medium: 'Facebook',
  127. utm_campaign: 'Some Campaign',
  128. utm_term: 'foo',
  129. }
  130. )
  131. })
  132. it('utm-tags user property is set for session', function (ctx) {
  133. sinon.assert.calledWith(
  134. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  135. ctx.req.session,
  136. 'utm-tags',
  137. 'N/A;Facebook;Some Campaign;foo'
  138. )
  139. })
  140. })
  141. describe('with some UTM tags and additional parameters in query', function () {
  142. beforeEach(function (ctx) {
  143. ctx.req.url =
  144. '/project?utm_medium=Facebook&utm_campaign=Some%20Campaign&other_param=some-value'
  145. ctx.req.query = {
  146. utm_medium: 'Facebook',
  147. utm_campaign: 'Some Campaign',
  148. other_param: 'some-value',
  149. }
  150. ctx.middleware(ctx.req, ctx.res, ctx.next)
  151. })
  152. it('user is redirected', function (ctx) {
  153. assert.isTrue(ctx.res.redirected)
  154. assert.equal('/project?other_param=some-value', ctx.res.redirectedTo)
  155. })
  156. it('next middleware is not executed', function (ctx) {
  157. sinon.assert.notCalled(ctx.next)
  158. })
  159. it('page-view event is recorded for session', function (ctx) {
  160. sinon.assert.calledWith(
  161. ctx.AnalyticsManager.recordEventForSession,
  162. ctx.req.session,
  163. 'page-view',
  164. {
  165. path: '/project',
  166. utm_medium: 'Facebook',
  167. utm_campaign: 'Some Campaign',
  168. }
  169. )
  170. })
  171. it('utm-tags user property is set for session', function (ctx) {
  172. sinon.assert.calledWith(
  173. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  174. ctx.req.session,
  175. 'utm-tags',
  176. 'N/A;Facebook;Some Campaign;N/A'
  177. )
  178. })
  179. })
  180. })