Skip to content

Commit f4f6f2b

Browse files
author
Asa Schachar
authored
Merge pull request #38 from optimizely/project-js-clear-ls
Project JS localStorage Example
2 parents a7bc254 + 34b8991 commit f4f6f2b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Project-JS/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Project JS
2+
3+
Project Javascript is an advanced configuration setting that provides a code field for JS to be executed before the Optimizely snippet's core logic begins executing. To learn more view our [documentation](https://help.optimizely.com/Set_Up_Optimizely/Project_Settings%3A_jQuery_and_Project_JavaScript_settings_in_Optimizely_X#Project_JavaScript). This repo contains a collection of code samples and reference implementations for extending Optimizely functionality via Project JS.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# clear_localstorage.js
2+
3+
Optimizely uses persistent visitor-level [cookies](https://help.optimizely.com/Set_Up_Optimizely/Cookies_and_localStorage_in_the_Optimizely_snippet#Cookies) and [localStorage](https://help.optimizely.com/Set_Up_Optimizely/Cookies_and_localStorage_in_the_Optimizely_snippet#localStorage) to uniquely identify visitors, track their actions, and deliver consistent experiences across page loads.
4+
5+
If in the course of your Optimizely usage you would like to clear stored values associated with Optimizely from your users' localStorage, for storage size management, compliance or other reasons, this code sample provides an example function that can be called form Project JS or invoked as a window-level function to do so. Specifically the function will iterate through all localStorage keys and clear any key:value pairs with the substring 'optimizely' in the keyname.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*Exposing a window-level function to clear localStorage values who's key includes the substring 'optimizely'
2+
*/
3+
window.clearOptlyLocalStorage = function(){
4+
for (var key in window.localStorage){
5+
if(key.indexOf('optimizely') !== -1){
6+
window.localStorage.removeItem(key)
7+
}
8+
}
9+
}
10+
//The function can be called from Optimizely Project JS based on an available boolean flag, such as a cookie's value
11+
var should_clear = window.customer_storage_flag_boolean;
12+
if(should_clear){
13+
clearOptimizelyLocalStorage();
14+
}

0 commit comments

Comments
 (0)