Advertisement
vapvarun

Set UTM parameters inside cookies

Aug 31st, 2021
1,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script src="https://cdn.jsdelivr.net/npm/js-cookie@beta/dist/js.cookie.min.js"></script>
  2.  
  3. <script type="module">
  4.  
  5. // Parse the URL
  6. function getParameterByName(name) {
  7.     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  8.     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  9.         results = regex.exec(location.search);
  10.     return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  11. }
  12. // Give the URL parameters variable names
  13. var source = getParameterByName('utm_source');
  14. var medium = getParameterByName('utm_medium');
  15. var campaign = getParameterByName('utm_campaign');
  16.  
  17. // Set the cookies
  18. if(Cookies.get('utm_source') == null || Cookies.get('utm_source') == "") {
  19. Cookies.set('utm_source', source);
  20. }
  21. if(Cookies.get('utm_medium') == null || Cookies.get('utm_medium') == "") {
  22. Cookies.set('utm_medium', medium);
  23. }
  24. if(Cookies.get('utm_campaign') == null || Cookies.get('utm_campaign') == "") {
  25. Cookies.set('utm_campaign', campaign);
  26. }
  27.  
  28.  
  29. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement