UserAnalyticsSDK

API Reference

Complete reference for all User Analytics SDK methods and classes.

📚 Table of Contents

AnalyticsTracker

The main class for interacting with the User Analytics SDK.

Package

com.analytics.analyticstracker.AnalyticsTracker

Import

import com.analytics.analyticstracker.AnalyticsTracker;

Initialization Methods

init(String baseUrl, String apiKey)

Initializes the SDK with custom server URL and API key.

Parameters:

Example:

AnalyticsTracker.init("https://d1xb34m3k0zeus.cloudfront.net/", "ak_your_api_key");

Notes:

init(String apiKey)

Initializes the SDK with API key only (uses default server URL).

Parameters:

Example:

AnalyticsTracker.init("ak_your_api_key");

Note: This method uses a default base URL. Use the two-parameter version for production.

Event Tracking

trackEvent(String userId, String actionName, Map<String, Object> properties)

Tracks a custom user event with properties.

Parameters:

Example:

Map<String, Object> properties = new HashMap<>();
properties.put("category", "ספורט");
properties.put("subcategory", "כדורגל");
properties.put("item", "כריסטיאנו רונאלדו");

AnalyticsTracker.trackEvent("user123", "click_item", properties);

Common Event Names:

Property Types:

Screen Time Tracking

startScreen(String screenName)

Starts tracking time spent on a screen.

Parameters:

Example:

@Override
protected void onResume() {
    super.onResume();
    AnalyticsTracker.startScreen("MainActivity");
}

Best Practices:

endScreen(String userId)

Ends screen time tracking and sends data to server.

Parameters:

Example:

@Override
protected void onPause() {
    super.onPause();
    AnalyticsTracker.endScreen("user123");
}

Best Practices:

trackScreenTime(String userId, String screenName, long startTimeMillis, long endTimeMillis, long durationMillis)

Manually track screen time with specific timestamps.

Parameters:

Example:

long startTime = System.currentTimeMillis();
// ... user interaction ...
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;

AnalyticsTracker.trackScreenTime("user123", "GameScreen", startTime, endTime, duration);

Note: This method is for advanced use cases. Use startScreen()/endScreen() for normal tracking.

User Management

loginUser(LoginRequest request, Callback callback)

Authenticates a user with the analytics system.

Parameters:

Example:

LoginRequest loginRequest = new LoginRequest("user@myinterest.com", "password");

AnalyticsTracker.loginUser(loginRequest, new Callback<AuthResponse>() {
    @Override
    public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
        if (response.isSuccessful()) {
            AuthResponse authResponse = response.body();
            // Handle successful login
        }
    }

    @Override
    public void onFailure(Call<AuthResponse> call, Throwable t) {
        // Handle login error
    }
});

updateUserName(String userId, String firstName, String lastName, Callback callback)

Updates user’s name information.

Parameters:

Example:

AnalyticsTracker.updateUserName("user123", "John", "Doe", new Callback<Void>() {
    @Override
    public void onResponse(Call<Void> call, Response<Void> response) {
        if (response.isSuccessful()) {
            // Name updated successfully
        }
    }

    @Override
    public void onFailure(Call<Void> call, Throwable t) {
        // Handle error
    }
});

Data Models

ActionEvent

Represents a user action event.

Fields:

ScreenTime

Represents screen time data.

Fields:

LoginRequest

Login request data.

Fields:

AuthResponse

Authentication response.

Fields:

🔧 Configuration

Error Handling

The SDK handles errors gracefully:

Logging

The SDK uses Android’s Log system:

Log.d("AnalyticsTracker", "Event sent successfully");
Log.e("AnalyticsTracker", "Network error occurred");

Log Tags:

Thread Safety

📱 Best Practices

User ID Management

// Good: Consistent user ID
String userId = UserManager.getCurrentUserId();
AnalyticsTracker.trackEvent(userId, "action", properties);

// Bad: Hardcoded or random IDs
AnalyticsTracker.trackEvent("user123", "action", properties);

Event Naming

// Good: Descriptive, consistent naming
AnalyticsTracker.trackEvent(userId, "click_category", properties);

// Bad: Vague or inconsistent naming
AnalyticsTracker.trackEvent(userId, "click", properties);

Property Structure

// Good: Structured, meaningful properties
Map<String, Object> properties = new HashMap<>();
properties.put("category", "ספורט");
properties.put("subcategory", "כדורגל");
properties.put("item", "כריסטיאנו רונאלדו");
properties.put("position", 0);

// Bad: Unstructured or meaningless properties
Map<String, Object> properties = new HashMap<>();
properties.put("data", "some_value");

🚨 Error Codes

Common error scenarios and their handling:

Error Description Solution
API Key not initialized SDK not initialized Call init() first
Network error Connection failed Check internet connection
Invalid API key Wrong or expired key Verify key in dashboard
Server error 500 Backend issue Contact support

For more examples, see Examples Documentation