I was reading the country of the visitor from local storage using localStorage.getItem I was reading this info directly form Google CMP.
console.log("Country code: " localStorage.getItem('vpb-location'));
var countrycodeckeck = localStorage.getItem('vpb-location');
if (
countrycodeckeck !== "PH" && //philippines
countrycodeckeck !== "TH" //thailand
) {
The problem is that now Google changed the key from vpb-location to vpbg and now contains more info.
Example:
var countrycodeckeck = localStorage.vpbg;
console.log(countrycodeckeck);
{"countryCode":"PL","isEU":true,"ts":1641125302501}
Can you please tell me how to modify our code to check the “countryCode” stored in the local storage?
CodePudding user response:
The data in the storage key is encoded as JSON. To access the information you need to decode the JSON to usable JavaScript with JSON.parse().
// Get the data from the storage.
const vpbg = localStorage.getItem('vpbg');
// Only proceed of there is actually data.
if (vpbg !== null) {
// Parse the JSON to an object.
const data = JSON.parse(vpbg);
// data is now an object with properties that you can access.
const countryCodeCheck = data.countryCode;
if (
countryCodeCheck !== "PH" && //philippines
countryCodeCheck !== "TH" //thailand
) {
// Do something if it checks out.
}
}
CodePudding user response:
You further append it with a dot to access those properties:
localStorage.vpbg.countryCode