Client-SideUnity SDKReference

Best Practices

Recommended practices for integrating the Unity SDK

Version:

Follow these best practices to get the most out of the AudienceLab SDK.

Initialization

The SDK initializes automatically. Ensure your token is configured before the first scene loads.

Event Timing

  • Purchase events: Send immediately after successful purchase confirmation
  • Ad events: Send after the ad completes (not when it starts)
  • Custom events: Send at the moment the action occurs

Deduplication

Use dedupeKey or tr_id for events that might be sent multiple times:

// Use transaction ID for purchases
AudiencelabSDK.SendPurchaseEvent(
    id: "premium_pack",
    name: "Premium Pack",
    value: 9.99,
    currency: "USD",
    status: "Completed",
    tr_id: "unique_transaction_id"
);

// Use unique keys for custom events
AudiencelabSDK.SendCustomEvent("purchase_flow_started",
    new { product_id = "premium_pack" },
    dedupeKey: $"flow_{sessionId}_{productId}"
);

User Properties

  • Set properties early (e.g., after user login)
  • Update properties when user state changes
  • Use blacklisted properties for PII hashes only
// Set after login
AudiencelabSDK.SetUserProperty("user_tier", "premium");
AudiencelabSDK.SetUserProperty("signup_date", "2024-01-15");

// Update when state changes
AudiencelabSDK.SetUserProperty("current_level", 42);

Testing

  1. Enable ShowDebugLog during development
  2. Use the Debug Overlay to verify events are being sent
  3. Test in both Editor and on-device builds
  4. Verify release builds with ProGuard rules

Common Patterns

Track Tutorial Completion

public void OnTutorialComplete()
{
    AudiencelabSDK.SendCustomEvent("tutorial_complete", new {
        total_time_seconds = tutorialTimer.TotalSeconds,
        skipped_steps = skippedSteps
    });
}

Track Purchase Flow

public void OnPurchaseSuccess(PurchaseResult result)
{
    AudiencelabSDK.SendPurchaseEvent(
        id: result.ProductId,
        name: result.ProductName,
        value: result.Price,
        currency: result.Currency,
        status: "Completed",
        tr_id: result.TransactionId
    );
}