Tracking Guest User IP Address and Device Type in Salesforce Experience Cloud
Capturing visitor details like IP address and device type is a common requirement for analytics, auditing, personalization, and security logging in Experience Cloud sites. However, when working with guest users, this becomes tricky because standard session methods don’t work for anonymous visitors.
In this post, we’ll build a working solution using:
- Visualforce – access headers
- Apex – expose data
- LWC – detect device
🚧 The Problem
Most developers try:
Auth.SessionManagement.getCurrentSession().get('SourceIp')
But this fails for guest users because they don’t have a session.
Cookies also aren’t reliable:
- May not exist
- Can be blocked
- Privacy settings vary
✅ Solution Architecture
| Layer | Purpose |
|---|---|
| Visualforce | Read request headers |
| Apex | Expose IP to LWC |
| LWC | Detect device type |
Step 1 — Visualforce Controller
public class ViewIPAddressController{
public String ipAddress{get; set;}
public ViewIPAddressController(){
ipAddress = ApexPages.currentPage().getHeaders()
.get('True-Client-IP')?.get('X-Salesforce-SIP');
if(String.isEmpty(ipAddress)){
ipAddress = ApexPages.currentPage().getHeaders()
.get('True-Client-IP')?.get('X-Forwarded-For');
}
}
}
Step 2 — Visualforce Page
<apex:page controller="ViewIPAddressController" showHeader="false">
<ipAddress>{!ipAddress}</ipAddress>
</apex:page>
This page outputs the IP so Apex can read it.
Step 3 — Apex Service
public with sharing class GuestInfoService {
@AuraEnabled(cacheable=true)
public static String getGuestIp(){
String ipAddress = (new PageReference('/apex/ViewIPAddress'))
.getContent()
.toString()
.substringBetween('<ipAddress>', '</ipAddress>');
return ipAddress;
}
}
Step 4 — Device Detection in LWC
Import Form Factor API:
import FORM_FACTOR from '@salesforce/client/formFactor';
LWC JS
import { LightningElement, wire } from 'lwc';
import getGuestIp from '@salesforce/apex/GuestInfoService.getGuestIp';
import FORM_FACTOR from '@salesforce/client/formFactor';
export default class GuestInfo extends LightningElement {
deviceType;
ipAddress;
connectedCallback() {
if (FORM_FACTOR === 'Small') {
this.deviceType = 'Mobile';
} else if (FORM_FACTOR === 'Medium') {
this.deviceType = 'Tablet';
} else {
this.deviceType = 'Desktop';
}
}
@wire(getGuestIp)
wiredIp({ data, error }) {
if (data) {
this.ipAddress = data;
} else if (error) {
this.ipAddress = 'Not available';
}
}
}
🔐 Required Permissions
- Apex Class: GuestInfoService
- Apex Class: ViewIPAddressController
- Visualforce Page: ViewIPAddress
📊 Use Cases
- Analytics
- Fraud detection
- Geo tracking
- Audit logging
- Security monitoring
⚡ Why This Works
- LWC cannot access headers
- Apex cannot read guest session
- Visualforce can read headers
LWC → Apex → Visualforce → Headers → IP
🏁 Final Result
You can reliably capture:
IP Address + Device Type
Even for anonymous users.
💡 Pro Tip
Store visitor data in a custom object:
Guest_Visit__c
- IP__c
- Device__c
- Timestamp__c
⭐ Conclusion
Tracking anonymous visitor data isn’t straightforward, but this Visualforce bridge technique allows you to capture guest IP addresses and combine them with LWC device detection for powerful insights.
Lightweight. Secure. Reliable.

Post a Comment