plans.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import '../../../marketing'
  2. import '../../../features/plans/group-plan-modal'
  3. import * as eventTracking from '../../../infrastructure/event-tracking'
  4. import getMeta from '../../../utils/meta'
  5. let currentView = 'monthly'
  6. let currentCurrencyCode = getMeta('ol-recommendedCurrency')
  7. function setUpViewSwitching(liEl) {
  8. const view = liEl.getAttribute('data-ol-view-tab')
  9. liEl.querySelector('a').addEventListener('click', function (e) {
  10. e.preventDefault()
  11. eventTracking.send('subscription-funnel', 'plans-page', `${view}-prices`)
  12. document.querySelectorAll('[data-ol-view-tab]').forEach(el => {
  13. if (el.getAttribute('data-ol-view-tab') === view) {
  14. el.classList.add('active')
  15. } else {
  16. el.classList.remove('active')
  17. }
  18. })
  19. document.querySelectorAll('[data-ol-view]').forEach(el => {
  20. el.hidden = el.getAttribute('data-ol-view') !== view
  21. })
  22. currentView = view
  23. updateLinkTargets()
  24. })
  25. }
  26. function setUpCurrencySwitching(linkEl) {
  27. const currencyCode = linkEl.getAttribute('data-ol-currencyCode-switch')
  28. linkEl.addEventListener('click', function (e) {
  29. e.preventDefault()
  30. document.querySelectorAll('[data-ol-currencyCode]').forEach(el => {
  31. el.hidden = el.getAttribute('data-ol-currencyCode') !== currencyCode
  32. })
  33. currentCurrencyCode = currencyCode
  34. updateLinkTargets()
  35. })
  36. }
  37. function setUpSubscriptionTracking(linkEl) {
  38. const plan =
  39. linkEl.getAttribute('data-ol-tracking-plan') ||
  40. linkEl.getAttribute('data-ol-start-new-subscription')
  41. linkEl.addEventListener('click', function () {
  42. const customLabel = linkEl.getAttribute('data-ol-tracking-label')
  43. const computedLabel = currentView === 'annual' ? `${plan}_annual` : plan
  44. const label = customLabel || computedLabel
  45. eventTracking.sendMB('plans-page-start-trial')
  46. eventTracking.send('subscription-funnel', 'sign_up_now_button', label)
  47. })
  48. }
  49. function updateLinkTargets() {
  50. document.querySelectorAll('[data-ol-start-new-subscription]').forEach(el => {
  51. if (el.hasAttribute('data-ol-has-custom-href')) return
  52. const plan = el.getAttribute('data-ol-start-new-subscription')
  53. const view = el.getAttribute('data-ol-item-view') || currentView
  54. const suffix = view === 'annual' ? `-annual` : `_free_trial_7_days`
  55. const planCode = `${plan}${suffix}`
  56. const location = el.getAttribute('data-ol-location')
  57. el.href = `/user/subscription/new?planCode=${planCode}&currency=${currentCurrencyCode}&itm_campaign=plans&itm_content=${location}`
  58. })
  59. }
  60. document.querySelectorAll('[data-ol-view-tab]').forEach(setUpViewSwitching)
  61. document
  62. .querySelectorAll('[data-ol-currencyCode-switch]')
  63. .forEach(setUpCurrencySwitching)
  64. document
  65. .querySelectorAll('[data-ol-start-new-subscription]')
  66. .forEach(setUpSubscriptionTracking)
  67. updateLinkTargets()