firstImpression.js: A micro-library for detecting new visitors
firstImpression.js is a micro/nano-library (1 kb minified) that answers the simple question, “Has this user visited this site before?” The detection doesn’t require much logic, so the majority of the code is just a Plain JavaScript port of the popular jquery.cookie plugin.
Using it
Writing and reading your own cookies is easy enough but if you want to operate at a slightly higher level firstImpression.js offers a simple API for detecting first-time visitors.
// Basic usage if ( firstImpression() ) { console.log('New user'); } // Specify cookie name if ( firstImpression('foo') ) { console.log('New user'); } // Specify cookie name and expiration in days if ( firstImpression('foo', 365) ) { console.log('New user'); } |
Some stuff:
- firstImpression() returns true for a new user, false for a returning user.
- Calling firstImpression() also sets a cookie if one does not already exist.
- The default cookie name is “_firstImpression” and the default expiration is 2 years (730 days).
- You can specify your own cookie names if you want to manage multiple instances of firstImpression across a single site.
- If you want to check if someone has visited within a certain time frame you can specify the cookie expiration (in days).
Deleting Cookies
If you need to delete cookies you can do so via firstImpression.js like this:
// Remove default cookie firstImpression(null); // Remove custom named cookie firstImpression('foo', null); |
Browser Support
This should work in any browser that supports cookies. Tested briefly in Chrome, Firefox, Opera, IE6-10, iOS, Android, and Opera Mobile.
Discussion
Thanks for sharing.
This is just what I was looking for, for my new project!
Keep up the good work.
Great!
Thanks!
Its useful.
thanks for sharing
Super sweet plugin – works fantastically.