This guide explains how to programmatically check and respond to the visibility state of CustomerGlu widgets by accessing the __GLU_NUDGE_CLICK property stored in localStorage.
Overview
When a CustomerGlu widget reaches its completed state, it stores interaction data in localStorage under the key __GLU_NUDGE_CLICK. By default, the widget is automatically hidden when the click count is 1 or more.
Important Notes:
This data is also stored on the backend and synced across devices
The visibility threshold can be customized through Visibility Conditions under Widget Configuration view for a campaign
Data Structure
The __GLU_NUDGE_CLICK property uses the following structure:
entrypointId: The ID representing the entry point where the widget is displayed
campaignId: The ID of the campaign associated with the widget
clickedCount: The number of times the widget was clicked (widget is hidden when this equals or exceeds the configured threshold, default is 1)
How to Access Widget Visibility?
Basic Code Example
// Read and parse the localStorage data
function getWidgetData() {
const keyName = "__GLU_NUDGE_CLICK";
const storedData = localStorage.getItem(keyName);
if (storedData) {
try {
return JSON.parse(storedData);
} catch (error) {
console.error("Error parsing widget data:", error);
return null;
}
}
return null;
}
// Check if a specific widget should be visible
function isWidgetVisible(userId, entrypointId, campaignId) {
const data = getWidgetData();
if (!data) {
// No data means the widget hasn't been clicked yet
return true;
}
// Navigate through the nested structure to get clickCount
// Using optional chaining for safety
const clickCount = data[userId]?.[entrypointId]?.[campaignId]?.clickedCount || 0;
// Widget is hidden when clickCount is 1 or more (default configuration)
// Note: This threshold may be different if configured in the CustomerGlu dashboard
return clickCount < 1;
}
Use Cases
1. Conditional UI Rendering
function updateUI() {
const userId = "user-123"; // Your actual user ID
const entrypointId = "entry-456"; // Your entrypoint ID
const campaignId = "campaign-789"; // Your campaign ID
if (isWidgetVisible(userId, entrypointId, campaignId)) {
// Show related UI elements
document.getElementById("related-element").style.display = "block";
} else {
// Hide related UI elements or show alternatives
document.getElementById("related-element").style.display = "none";
document.getElementById("alternative-element").style.display = "block";
}
}
Avoid checking localStorage in tight loops or high-frequency events
Consider caching the result if checking frequently:
javascriptCopy// Cache visibility state with a simple refresh mechanism
const visibilityCache = {
data: {},
timestamp: 0,
ttl: 5000, // 5 seconds cache validity
getVisibility(userId, entrypointId, campaignId) {
const key = `${userId}:${entrypointId}:${campaignId}`;
const now = Date.now();
// Refresh cache if expired
if (now - this.timestamp > this.ttl) {
this.data = {};
this.timestamp = now;
}
// Return cached value if exists
if (this.data[key] !== undefined) {
return this.data[key];
}
// Calculate and cache new value
const isVisible = isWidgetVisible(userId, entrypointId, campaignId);
this.data[key] = isVisible;
return isVisible;
}
};
3. Cross-Device Considerations
Since the widget state is synced across devices via the backend, users will generally see consistent widget visibility across their devices. However, there might be a short delay in synchronization.
Troubleshooting
Widget Visibility Inconsistencies
If the widget visibility state seems inconsistent:
Verify you're using the correct IDs (userId, entrypointId, campaignId)