3

I need to set a cookie whenever a user clicks through one of our affiliate links and lands on our site with "src=uni" in the URL. The URLs will look something like this:

http://www.myadmin.com?src=uni&utm_source=uni&utm_content=[publisher_ID]

Function to create cookie:

 function SetCookie() {
            var url = window.location.href;
            if(url.indexOf('?src' + uni) = 1)
                document.cookie="QueryCookie";
    }

Can somebody help me by telling where I am going wrong in creating this Cookie based on query parameters?

Programmermid
  • 588
  • 3
  • 9
  • 30

2 Answers2

3

A few things here:

function SetCookie() {
    var url = window.location.search;
    if(url.indexOf('?src=uni') !== -1)
        document.cookie="src=uni";
}

1) Use location.search to narrow down your range, not necessary, but less room for error,

2) Use !== -1 to test the indexOf method. indexOf returns "-1" if it does not find a match. And "0" if it finds a match at the beginning of the string. The string is "zero indexed" which means the first character in the string is in position "0".

3) Add the equal sign = along with your parameter name: src=.

4) Also, use the string "uni" if that is what you're looking for, rather than a variable named uni. If "src" can be a variety of values, then we'll need to add some more logic to account for that.

5) And when assigning to document.cookie use key/value pairs as in: key=value.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
  • Thanks Man, I fixed that and its still not working. I want to store key as 'src' and its value as 'uni' but I am not sure why the above code is also not responding. – Programmermid Apr 22 '15 at 17:12
  • @Programmermid Okay, I updated my response to set a cookie named "src" to the value "uni". Is that what you're looking for? – bloodyKnuckles Apr 22 '15 at 17:15
  • Ya it worked, but a quick question will this cookie be set whenever src=uni in the url below: http://www.myadmin.com?src=uni&utm_src=uni&utm_content=[publisher_ID]? – Programmermid Apr 22 '15 at 17:20
  • @Programmermid Yes, either of those occurrences of "src=uni" would have triggered it, even "utm_src=uni", so I changed my response to limit it to "?src=uni". Now only the initial "src=uni" will trigger it. – bloodyKnuckles Apr 22 '15 at 17:34
  • Thanks Man that was a big help from your side. – Programmermid Apr 22 '15 at 17:45
2

First thing you need to fix is:

if(url.indexOf('?src' + uni) = 1)

should be (this checks that it exists at index 1):

if(url.indexOf('?src=' + uni) === 1)

or (this checks it exists at all)

if(url.indexOf('?src=' + uni) !== -1)

Next, you need to set src to the uni and make it accessible to the entire site:

document.cookie="src="+uni+"; path=/; domain=.myadmin.com";

Adding the path=/ and domain=.myadmin.com will allow you to access the cookie at all paths on that domain, and the domain part lets it be accessible on all subdomains (i.e. www.myadmin.com as well as blog.myadmin.com, etc)

so all together:

    function SetCookie() {
        var url = window.location.href;
        if(url.indexOf('?src='+uni) !== -1)
            document.cookie="src="+uni+"; path=/; domain=.myadmin.com";
    }

Here is some basic info:

http://www.w3schools.com/js/js_cookies.asp

Or the more in depth, accurate documentation:

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

WakeskaterX
  • 1,408
  • 9
  • 21
  • Thanks Man, I fixed that and its still not working. Do you have some other suggestions that I should look into to make this work? – Programmermid Apr 22 '15 at 17:03
  • Entire thing is I want to Set a cookie when the src=uni and then if user directed from this link makes a purchase, our site should look for this cookie and if it's present, then the tag I have in our ReceiptPage should have 'FIRESRC' : 'TRUE'. If the cookie is not present, the tag should have 'FIRESRC' : 'FALSE'. So I just want to store Key as 'SRC' and its value as 'UNI' in this cookie. – Programmermid Apr 22 '15 at 17:08
  • You can just set it to true then. But you should expire the cookie as well. The link I showed shows how to do that. You likely wouldn't want it to stay there forever. – WakeskaterX Apr 22 '15 at 17:10
  • Don't forget the "=" sign either, just added that, plus is "uni" the word UNI or a variable that changes? – WakeskaterX Apr 22 '15 at 17:12
  • "UNI" is a variable that I have in my Receipt page which will track the sale details. – Programmermid Apr 22 '15 at 17:15
  • Ah okay, then disregard the string comments. That should work, are you using this on the same site? – WakeskaterX Apr 22 '15 at 17:17
  • Please consider a reliable resource rather than w3schools. – Benjamin Gruenbaum Apr 22 '15 at 17:23
  • I figured it would be the easiest resource to read but I included the MDN page as well. – WakeskaterX Apr 22 '15 at 17:24
  • No I am using this cookie for different sites. Also if I do document.cookie="src=uni"; will this create a cookie with its key as src and value as uni? – Programmermid Apr 22 '15 at 17:37
  • Cookies are stored for the current document, you wouldn't be able to set one in one site and reference it in another site, if that's what you're getting at. Also, yes, the string after the "=" sign, whether that is a static string or a variable concatenated to the previous string will be stored with that value. – WakeskaterX Apr 22 '15 at 17:45
  • Ya but can I check a cookie which was created on homepage on the receipt page? – Programmermid Apr 22 '15 at 17:48
  • You need to make sure the path and domain are set correctly so you can use it across the whole site. I'll update above. – WakeskaterX Apr 22 '15 at 17:53