title: Set a Javascript Cookie based on URL Params
body: <p>I'm curious if someone can help a very new Javascript user make sense of how to set a cookie, based on specific URL parameters. I see that pulling the data from the URL using JavaScript is covered in this post: 
  <a href="https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript">How can I get query string values in JavaScript?</a></p>
  
  <p>But I can not figure out how to pull that information into a cookie to store the information throughout a users session on the site. </p>
  
  <p>I would like to grab 3 main URL parameters: 
  utm_source
  utm_medium
  utm_campaign </p>
  
  <p>And then store them in a cookie in Google Tag Manager using Javascript. </p>
  
  <p>I can not wrap my head around making this happen. Any help would be greatly appreciated. Sorry I dont have much code to show for reference, but I have been experimenting ( and failing ) for hours now. </p>
  
  <p>Thank you so much for any insight on this. </p>
  
  <p>Cheer, 
  Melissa</p>
  
  <p>Edit: <br>
  Sorry...I wasn't expecting someone to write it for me, I just didn't think my very failed attempts would help anyone see what I was trying to do so I just explained. </p>
  
  <p>Here is my code as of now, and I know it's sort of working. I'm editing a previous cookie that stores the site referrer in a cookie. So as it stands right now, the cookie stores the referrer on the first pageview, then if you go to a different page it will show the <code>{{utm_medium}}</code> and continue to show that throughout the visit. I would like for it to not show the referrer, but output a cookie that displays <code>{{utm_source}} | {{utm_medium}} | {{utm_campaign}}</code> if that's even possible... </p>
  
  <p>Thank you again for any help or pointers or articles. I really appreciate it. </p>
  
  <pre><code>&lt;script&gt; //get referrer info and shorten it
  var ref = {{Referrer}}
  
  function extractDomain(url) {
    var domain;
    //find &amp; remove protocol (http, ftp, etc.) and get domain
    if (url.indexOf("://") &gt; -1) {
      domain = url.split('/')[2];
    } else {
      domain = url.split('/')[0];
    }
    //find &amp; remove port number
    domain = domain.split(':')[0];
    return domain;
  }
  ref = extractDomain(ref);
  //create cookie 
  function createCookie(name, value, days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  }
  var cookie = "";
  //check if UTMs are present and set cookie content to the source utm  
  if ({{utm_source}}) {
    createCookie("utmsource", cookie + {{utm_source}}, 1000)
  } else if ({{utm_medium}}) {
    createCookie("utmsource", cookie + "Email", 1000)
      //check if referrer is present and set cookie content to the referrer
  } else if ({{utm_campaign}}) {
    createCookie("utmsource", cookie + "{{utm_campaign}}", 1000)
  } else if {
    createCookie("utmsource", cookie + "Email", 1000)
  };
  &lt;/script&gt;
  </code></pre>