utm.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function getRefQueryParam(name) {
  2. name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  3. var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
  4. var results = regex.exec(location.search);
  5. return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  6. };
  7. var utmParamQueryString = '',
  8. utmParamQueryStringTrimmed = '',
  9. utm_source = '',
  10. utm_medium = '',
  11. utm_content = '',
  12. utm_campaign = '',
  13. utm_term = '';
  14. (function() {
  15. utm_source = getRefQueryParam("utm_source");
  16. utm_medium = getRefQueryParam("utm_medium");
  17. utm_content = getRefQueryParam("utm_content");
  18. utm_campaign = getRefQueryParam("utm_campaign");
  19. utm_term = getRefQueryParam("utm_term");
  20. if (utm_source) {
  21. utmParamQueryString += '&utm_source=' + utm_source;
  22. }
  23. if (utm_medium) {
  24. utmParamQueryString += '&utm_medium=' + utm_medium;
  25. }
  26. if (utm_content) {
  27. utmParamQueryString += '&utm_content=' + utm_content;
  28. }
  29. if (utm_campaign) {
  30. utmParamQueryString += '&utm_campaign=' + utm_campaign;
  31. }
  32. if (utm_term) {
  33. utmParamQueryString += '&utm_term=' + utm_term;
  34. }
  35. if(utmParamQueryString.length > 0) {
  36. utmParamQueryString = utmParamQueryString.substring(1);
  37. utmParamQueryStringTrimmed = utmParamQueryString;
  38. utmParamQueryString = utmParamQueryString;
  39. }
  40. if (!utmParamQueryString) return;
  41. var navLinks = document.querySelectorAll('a');
  42. navLinks.forEach(function(item) {
  43. if (item.href.indexOf('/') === 0 || item.href.indexOf(location.host) !== -1) {
  44. if (item.href.indexOf('?') === -1) {
  45. item.href += '?';
  46. } else {
  47. item.href += '&';
  48. }
  49. item.href += utmParamQueryString;
  50. }
  51. });
  52. })();
  53. function ocard_redirect() {
  54. console.log('Redirect to ' + ocard_link); // test
  55. window.location.href = ocard_link;
  56. }
  57. // UTM -> Ocard
  58. var ocard_link = 'https://ocard.co/q?s=mkwbRm&qtk=bZ9nam&utm_source=web&utm_medium=booking&utm_campaign=booking&utm_content=booking';
  59. if (utmParamQueryString) {
  60. ocard_link = 'https://ocard.co/q?s=mkwbRm&qtk=bZ9nam&' + utmParamQueryString;
  61. }
  62. console.log('link = ' + window.location.href); // test
  63. console.log('utmParamQueryString = ' + utmParamQueryString); // test
  64. console.log('ocard_link = ' + ocard_link); // test
  65. var reservation = document.getElementsByClassName('reservation-href');
  66. for(var i = 0; i < reservation.length; i++) {
  67. reservation[i].addEventListener("click", ocard_redirect);
  68. }