Wednesday, June 26, 2013

Detection of first time visitor to the website using jquery.

Hi guys,


Today I was working with one requirement, in which I would find the first time visitor to my portal/website(I am using Liferay portal for website) for some business needs. Lets say to Wish like welcome note with beautiful picture or like asking user to agree terms and condition before going opening website, etc, etc.... I think most of the time we need this feature. Hence I am writing this posts, may be useful for somebody.


HOW TO DETECT FIRST TIME VISITOR TO THE WEBSITE???????

If we are using any web technologies or I would say smart frameworks like LIFERAY, we can make use of Jquery code to detect. So how???? Lets see how we can do in Liferay using themes, using Jquery in it. 

  • If you are using your custom theme, you can add the jquery code in main.js file. Which you can find in <theme-name>/docroot/_diffs/js/main.js
  • lets write jquery code, we can make use of cookies.
            lets give the cookie name to your site as  "myCookie", lets set the cookie name.
           
 if(!($.cookie('myCookie')))
  {
      alert("Welcome, Thanks for visiting");
      $.cookie('myCookie','visited', { path: '/', expires: 10000 });
  }
else
 {
     alert("Already visited.");
 }
That's all guys. Use the above code in main.js of your theme and you can detect the first time visitor.
IMPORTANT NOTES 
  1. To work with cookies of Jquery you should have the jquery.cookie.js plugin. You can find that in   jquery.cookie.js
  2. If you want to know more about attributes I have used for cookies you can go through Cookies Properties



No comments:

Post a Comment