Product Fruits works best after login. But if you want to implement Product Fruits to a site without authentication or before login, it is possible.
You still have to provide user information, Product Fruits won't start without a username. You can generate a random username
, store in in browser's localStorage
and use it later. It's also possible to hardcode a username but this will result in onboarding content that is only intended to show 'once per user' will not function properly.
This is an example of the username generator:
function makeid(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
let generatedUserName = localStorage.getItem('user');
if (!generatedUserName) {
generatedUserName = makeid(8);
localStorage.setItem('user', generatedUserName);
}
Later, when you initialize Product Fruits, use the generatedUserName
variable for the username
field of the Product Fruits user info object.
If the user logs in later and there is no reload (the login form and the following page work as a single page), call productFruits.identifyUser({ username: '<<REPLACE>>' })
to re-identify the user.
Please note, that using Product Fruits on sites without authentication can cause some of these situations: