0

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: How can I get query string values in JavaScript?

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.

I would like to grab 3 main URL parameters: utm_source utm_medium utm_campaign

And then store them in a cookie in Google Tag Manager using Javascript.

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.

Thank you so much for any insight on this.

Cheer, Melissa

Edit:
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.

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 {{utm_medium}} and continue to show that throughout the visit. I would like for it to not show the referrer, but output a cookie that displays {{utm_source}} | {{utm_medium}} | {{utm_campaign}} if that's even possible...

Thank you again for any help or pointers or articles. I really appreciate it.

<script> //get referrer info and shorten it
var ref = {{Referrer}}

function extractDomain(url) {
  var domain;
  //find & remove protocol (http, ftp, etc.) and get domain
  if (url.indexOf("://") > -1) {
    domain = url.split('/')[2];
  } else {
    domain = url.split('/')[0];
  }
  //find & 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)
};
</script>
Community
  • 1
  • 1
Melissaa
  • 1
  • 1
  • 3
  • Putting information in a cookie is the same no matter where the information came from. Once you extract the URL parameter into a variable, you can use it just like any other variable. – Barmar Jan 29 '16 at 17:40
  • 2
    If you've been experimenting for hours, you must have some code that you tried. Post one of your attempts, and we'll help you fix it, but we're not going to write it for you. – Barmar Jan 29 '16 at 17:42
  • I edited my post to show the code I'm working with right now. Sorry again. – Melissaa Jan 29 '16 at 17:55
  • What are those `{{...}}` things? That's not standard Javascript syntax. Where is your code that extracts URL parameters, using the code that you got from the other question? – Barmar Jan 29 '16 at 19:43
  • You say you don't want the cookie to show the referrer. I don't see anything that's adding the referrer to the cookie in the first place. – Barmar Jan 29 '16 at 19:45
  • Your use of `else if` means that only one of the branches will be taken, it won't combine all the `utm_XXX` parameters. I think you want them each to be indepdenent `if` statements. Also, the last `else if` is missing a condition to check -- it should just be `else`. – Barmar Jan 29 '16 at 19:47
  • `{{...}}` are google tag manager variable references. They pull in different user defined references like cookies and URL queries. I hope that helps! – Melissaa Jan 29 '16 at 19:51

1 Answers1

0

When you use cookie + something, you're not updating the cookie string. So each time you do this, you're just concatenating with the original, empty value of this string. Instead of calling setcookie multiple times, update the cookie string as you test the different variables, then call setcookie at the end with the combined value.

You shouldn't use else if between each test, since that will only add the second variable to the cookie if the first variable didn't exist. But you want all the variables put into the cookie.

var cookie = "";
if ({{utm_source}}) {
    cookie += {{utm_source}};
}

if ({{utm_medium}}) {
    cookie += ' | ' + {{utm_medium}};
} else {
    cookie += ' | Email';
}

if ({{utm_campaign}}) {
    cookie += ' | ' + {{utm_campaign}};
} else {
    cookie += ' | Email';
}

setcookie('utm_source', cookie, 1000);
Barmar
  • 741,623
  • 53
  • 500
  • 612