AnalyticsUTMTrackingMiddleware.test.mjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { assert, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockRequest from '../helpers/MockRequest.mjs'
  4. import MockResponse from '../helpers/MockResponse.mjs'
  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(vi)
  14. ctx.res = new MockResponse(vi)
  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.mjs',
  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.req.headers = {
  69. host: 'test-domain.overleaf.com',
  70. }
  71. ctx.middleware(ctx.req, ctx.res, ctx.next)
  72. })
  73. it('user is redirected', function (ctx) {
  74. assert.isTrue(ctx.res.redirected)
  75. assert.equal('/project', ctx.res.redirectedTo)
  76. })
  77. it('next middleware is not executed', function (ctx) {
  78. sinon.assert.notCalled(ctx.next)
  79. })
  80. it('page-view event is recorded for session', function (ctx) {
  81. sinon.assert.calledWith(
  82. ctx.AnalyticsManager.recordEventForSession,
  83. ctx.req.session,
  84. 'page-view',
  85. {
  86. path: '/project',
  87. utm_source: 'Organic',
  88. utm_medium: 'Facebook',
  89. utm_campaign: 'Some Campaign',
  90. utm_content: 'foo-bar',
  91. utm_term: 'overridden',
  92. domain: 'test-domain',
  93. }
  94. )
  95. })
  96. it('utm-tags user property is set for session', function (ctx) {
  97. sinon.assert.calledWith(
  98. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  99. ctx.req.session,
  100. 'utm-tags',
  101. 'Organic;Facebook;Some Campaign;foo-bar'
  102. )
  103. })
  104. })
  105. describe('with some UTM tags in query', function () {
  106. beforeEach(function (ctx) {
  107. ctx.req.url =
  108. '/project?utm_medium=Facebook&utm_campaign=Some%20Campaign&utm_term=foo'
  109. ctx.req.query = {
  110. utm_medium: 'Facebook',
  111. utm_campaign: 'Some Campaign',
  112. utm_term: 'foo',
  113. }
  114. ctx.req.headers = {
  115. host: 'test-domain.overleaf.com',
  116. }
  117. ctx.middleware(ctx.req, ctx.res, ctx.next)
  118. })
  119. it('user is redirected', function (ctx) {
  120. assert.isTrue(ctx.res.redirected)
  121. assert.equal('/project', ctx.res.redirectedTo)
  122. })
  123. it('next middleware is not executed', function (ctx) {
  124. sinon.assert.notCalled(ctx.next)
  125. })
  126. it('page-view event is recorded for session', function (ctx) {
  127. sinon.assert.calledWith(
  128. ctx.AnalyticsManager.recordEventForSession,
  129. ctx.req.session,
  130. 'page-view',
  131. {
  132. path: '/project',
  133. utm_medium: 'Facebook',
  134. utm_campaign: 'Some Campaign',
  135. utm_term: 'foo',
  136. domain: 'test-domain',
  137. }
  138. )
  139. })
  140. it('utm-tags user property is set for session', function (ctx) {
  141. sinon.assert.calledWith(
  142. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  143. ctx.req.session,
  144. 'utm-tags',
  145. 'N/A;Facebook;Some Campaign;foo'
  146. )
  147. })
  148. })
  149. describe('with some UTM tags and additional parameters in query', function () {
  150. beforeEach(function (ctx) {
  151. ctx.req.url =
  152. '/project?utm_medium=Facebook&utm_campaign=Some%20Campaign&other_param=some-value'
  153. ctx.req.query = {
  154. utm_medium: 'Facebook',
  155. utm_campaign: 'Some Campaign',
  156. other_param: 'some-value',
  157. }
  158. ctx.req.headers = {
  159. host: 'test-domain.overleaf.com',
  160. }
  161. ctx.middleware(ctx.req, ctx.res, ctx.next)
  162. })
  163. it('user is redirected', function (ctx) {
  164. assert.isTrue(ctx.res.redirected)
  165. assert.equal('/project?other_param=some-value', ctx.res.redirectedTo)
  166. })
  167. it('next middleware is not executed', function (ctx) {
  168. sinon.assert.notCalled(ctx.next)
  169. })
  170. it('page-view event is recorded for session', function (ctx) {
  171. sinon.assert.calledWith(
  172. ctx.AnalyticsManager.recordEventForSession,
  173. ctx.req.session,
  174. 'page-view',
  175. {
  176. path: '/project',
  177. utm_medium: 'Facebook',
  178. utm_campaign: 'Some Campaign',
  179. domain: 'test-domain',
  180. }
  181. )
  182. })
  183. it('utm-tags user property is set for session', function (ctx) {
  184. sinon.assert.calledWith(
  185. ctx.AnalyticsManager.setUserPropertyForSessionInBackground,
  186. ctx.req.session,
  187. 'utm-tags',
  188. 'N/A;Facebook;Some Campaign;N/A'
  189. )
  190. })
  191. })
  192. })