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 context as shown in the examples. If you are adding this code inside an Activity, replace context with this.
  • 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_open
  • install
  • update
  • Notification delivery and clicks.

5. 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

  1. Run the app on a device or emulator from Android Studio.
  2. Open Logcat and search for Blueshift.
  3. 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.