0

I have one website like* www_demo_com and i was advertised on youtube so how to know users where to come?

I'was tried using this.

location.host()

but this is not perfect for my problem.

Kaushik Makwana
  • 2,422
  • 9
  • 31
  • 50
  • Not sure what you're asking. Do you wish to know if your visitors came to your site by clicking a link on youtube? – xiix Feb 06 '16 at 11:32
  • i want to know my visitors where to comes? like he/she come from youtube or via goole or any other reference. – Kaushik Makwana Feb 06 '16 at 11:33
  • You can't really get where a user came from, but `window.location.hostname` ist the correct thingy. – Sainan Feb 06 '16 at 11:37
  • I'm not sure it's possible. You could try document.referrer, but I'm not sure how that works. Otherwise, there are services for it, or you could use referrer links, so when someone links to your site, they do it with the URL www.yoursite.com/?referrer=something. Then, you could use the URL to see whowas set as the referrer. – xiix Feb 06 '16 at 11:38
  • 1
    Possible duplicate of [Get HTTP Referrer on Redirection](http://stackoverflow.com/questions/4043196/get-http-referrer-on-redirection) – choz Feb 06 '16 at 11:39

2 Answers2

2

It is not clear what you are asking here - but I am going to assume you are asking - how do i know if someone came to my website from youtube

1) using document.referrer property - the documentation can be found here - note this will not always work (depends on a bunch of things)

you can do something like:

if (document.referrer && document.referrer != "")
{
    if(document.referrer.indexOf("youtube") > -1)
    {
        //here we know that the referrer value contains youtube - you should execute some logic here
    }
}

2) if you have given someone a referral link and told them to use that - you need to look for the unique string you gave them - for instance if you told them to go to www.yoursite.com/?ref=some-refferal-link-identifier you can do something like this:

if(window.location.href.indexOf("some-refferal-link-identifier") > -1)
{
    //here we know that the referrer value contains your unique ID - you should execute some logic here
}

All that being said - the best way is probably to use something like google analytics - obviously google built their core business on this stuff, you will be able to use their tools to track this very well.

IaMaCuP
  • 835
  • 5
  • 19
0

If you want to get the full URL then try

location.href

Prashant Agrawal
  • 660
  • 9
  • 24