Track events and manage user data
How to identify users and track events such as purchases, screen views, and custom actions.
This guide explains how to identify users and track events such as purchases, screen views, and custom actions.
1. Set customer information
Use the UserInfo class to store customer details in the SDK.
This ensures that all subsequent events are tied to the correct user.
Example code
import com.blueshift.model.UserInfo;
// Identify the user
UserInfo userInfo = UserInfo.getInstance(context);
// Set email
userInfo.setEmail("[email protected]");
// Set customer_id
userInfo.setRetailerCustomerId("customer:1234");
// Set additional key-value pairs
HashMap<String,Object> extras = new HashMap<>();
extras.put("verification_key", "[email protected]");
extras.put("event_signature", "e88f85c920...");
userInfo.setDetails(extras);
// Save after updates
userInfo.save(context);
import com.blueshift.model.UserInfo
// Identify the user
val userInfo = UserInfo.getInstance(context)
// Set email
userInfo.email = "[email protected]"
// Set customer_id
userInfo.retailerCustomerId = "customer:1234"
// Set additional key-value pairs
val extras = HashMap<String, Any>()
extras["verification_key"] = "[email protected]"
extras["event_signature"] = "e88f85c920..."
userInfo.setDetails(extras)
// Save after updates
userInfo.save(context)
Notes
- Use
contextas shown in the examples. If you are adding this code inside anActivity, replacecontextwiththis. - The values above are sample placeholders for testing. In production, replace them with the actual user’s email and customer ID when the user signs in.
2. Clear customer information
When a user logs out or unregisters, clear their details so no events are tied to them:
UserInfo.getInstance(context).clear(context);
UserInfo.getInstance(context).clear(context)
3. Track events
The SDK automatically tracks common events such as app installs and app opens. You can also track custom or business-specific events.
Track custom events
Use the trackEvent method to track custom events with specific attributes.
HashMap<String, Object> details = new HashMap<>();
details.put("name", "Ross Geller");
details.put("profession", "paleontologist");
Blueshift.getInstance(context).trackEvent("your_custom_event_name", details, true);
val details = HashMap<String, Any>()
details["name"] = "Ross Geller"
details["profession"] = "paleontologist"
Blueshift.getInstance(context).trackEvent("your_custom_event_name", details, true)
Track screen views
Use the trackScreenView method to log navigation inside your app.
Blueshift.getInstance(context).trackScreenView("Home Screen", false);
Blueshift.getInstance(context).trackScreenView("Home Screen", false)
Track purchase events
Use the trackEvent method to capture purchase details.
HashMap<String, Object> purchase = new HashMap<>();
purchase.put("order_id", "12345");
purchase.put("total", 99.99);
Blueshift.getInstance(context).trackEvent("purchase", purchase, false);
val purchase = HashMap<String, Any>()
purchase["order_id"] = "12345"
purchase["total"] = 99.99
Blueshift.getInstance(context).trackEvent("purchase", purchase, false)
4. Events tracked automatically
The SDK also tracks the following events without additional code:
app_openinstallupdate- Notification delivery and clicks.
5. Disable tracking
Control event tracking to respect user privacy and comply with regulations like GDPR and CCPA.
Enable or disable event tracking
Toggle event tracking on or off while maintaining other SDK functionality.
import com.blueshift.Blueshift
// Disable event tracking
Blueshift.setTrackingEnabled(false)
// Enable event tracking
Blueshift.setTrackingEnabled(true)
import com.blueshift.Blueshift;
// Disable event tracking
Blueshift.setTrackingEnabled(false);
// Enable event tracking
Blueshift.setTrackingEnabled(true);
Check current tracking status
Check whether event tracking is currently active.
val isTrackingEnabled = Blueshift.isTrackingEnabled()
if (isTrackingEnabled) {
// Event tracking is active
} else {
// Event tracking is disabled
}
boolean isTrackingEnabled = Blueshift.isTrackingEnabled();
if (isTrackingEnabled) {
// Event tracking is active
} else {
// Event tracking is disabled
}
Set tracking state during initialization
Configure the initial tracking state when setting up the SDK.
import com.blueshift.BlueshiftConfig
val config = BlueshiftConfig()
config.setEventTrackingEnabled(false) // Initialize with tracking disabled
config.setApiKey("your-api-key")
Blueshift.initialize(this, config)
import com.blueshift.BlueshiftConfig;
BlueshiftConfig config = new BlueshiftConfig();
config.setEventTrackingEnabled(false); // Initialize with tracking disabled
config.setApiKey("your-api-key");
Blueshift.initialize(this, config);
Important:When event tracking is disabled:
- Custom events and user actions are not recorded
- Push notifications continue to function normally
- User identification and device registration remain active
Privacy compliance implementation
Implement user consent controls for privacy regulations.
GDPR consent example:
// Handle user opt-out
fun onUserOptOut() {
Blueshift.setTrackingEnabled(false)
// Store user's privacy preference
Log.d("Privacy", "User opted out of event tracking")
}
// Handle user consent
fun onUserConsent() {
Blueshift.setTrackingEnabled(true)
// Store user's privacy preference
Log.d("Privacy", "User provided tracking consent")
}
// Handle user opt-out
public void onUserOptOut() {
Blueshift.setTrackingEnabled(false);
// Store user's privacy preference
Log.d("Privacy", "User opted out of event tracking");
}
// Handle user consent
public void onUserConsent() {
Blueshift.setTrackingEnabled(true);
// Store user's privacy preference
Log.d("Privacy", "User provided tracking consent");
}
6. Test your implementation
To verify that event tracking works, add a simple test event:
HashMap<String, Object> testData = new HashMap<>();
testData.put("test_key", "docs_test");
Blueshift.getInstance(MainActivity.this).trackEvent("test_custom_event", testData, true);
val testData = HashMap<String, Any>()
testData["test_key"] = "docs_test"
Blueshift.getInstance(this).trackEvent("test_custom_event", testData, true)
Verify logs
- Run the app on a device or emulator from Android Studio.
- Open Logcat and search for
Blueshift. - Look for a log entry such as:
EventManager: Inserting 1 batch event -> test_custom_event
If you see this log entry, the SDK is correctly tracking events.
Remove this test code before releasing your app.
Updated 17 days ago