Dynamic Offering

This page provides details about the dynamic offerings feature

Dynamic Offering let you define for a user which Offering they should see.



/// **Setting a Dynamic Offering**
/// Use this when you want to override the default plans shown on a specific screen or context.
let reference = "dynamic_offering_reference"
let targetPlanVendorId = "yearly_premium_01"
let specificOfferId = "YEARLY_PROMO_XYZ" // Optional: can be nil

print("Setting dynamic offering for reference: \(reference)...")
Purchasely.setDynamicOffering(reference: reference,
               planVendorId: targetPlanVendorId,
               offerVendorId: specificOfferId) { success in
  // This completion handler is called after the SDK attempts to store the offering.
  // It runs asynchronously.
  if success {
    print("Successfully set dynamic offering for \(reference).")
    // You might update UI or state here if needed
  } else {
    print("Failed to set dynamic offering for \(reference).")
    // Handle failure if necessary
  }
}

/// **Getting Current Dynamic Offerings (Synchronous)**
/// Useful for debugging or immediate checks within your code logic to see
/// which dynamic offerings are currently active in memory.
print("Checking dynamic offerings synchronously...")
let currentOfferings = Purchasely.getDynamicOfferings() // Synchronous call

if currentOfferings.isEmpty {
  print("No dynamic offerings currently set.")
} else {
  print("Current dynamic offerings:")
  for offering in currentOfferings {
    // Assuming PLYOffering has relevant properties like reference, planId, offerId
    print("- Ref: \(offering.reference), Plan: \(offering.planId), Offer: \(offering.offerId ?? "N/A")")
  }
}

/// **Getting Current Dynamic Offerings (Asynchronous)**
/// Use this if retrieving the offerings might involve I/O or other async work,
/// or simply to follow an asynchronous pattern.
print("Checking dynamic offerings asynchronously...")
Purchasely.getDynamicOfferings { currentOfferings in
  // This completion handler might be called asynchronously.
  if currentOfferings.isEmpty {
    print("Async fetch: No dynamic offerings currently set.")
  } else {
    print("Async fetch: Current dynamic offerings:")
    for offering in currentOfferings {
      print("- Ref: \(offering.reference), Plan: \(offering.planId), Offer: \(offering.offerId ?? "N/A")")
    }
  }
}

/// **Removing a Specific Dynamic Offering**
/// Call this when the specific context requiring an override is no longer valid.
let reference = "sample_reference"
print("Removing dynamic offering for reference: \(reference)...")
Purchasely.removeDynamicOffering(reference: reference)
print("Dynamic offering for \(reference) removed (if it existed).")

/// **Clearing All Dynamic Offerings**
/// Useful on user logout, or when you want to ensure all paywalls revert
/// to their default server-configured offerings.
print("Clearing all dynamic offerings...")
Purchasely.clearDynamicOfferings()
print("All dynamic offerings cleared.")
// ---------- Setting a Dynamic Offering ----------
val reference = "dynamic_offering_reference"
val planVendorId = "your_plan_vendor_id"      // Required: must be a valid plan vendor id
val offerVendorId = "your_offer_vendor_id"    // Optional: can be null


Purchasely.setDynamicOffering(
    reference = reference,
    planVendorId = planVendorId,
    offerVendorId = offerVendorId
) { success ->
    // This callback is invoked after the SDK attempts to store the offering
    if (success) {
        println("Successfully set dynamic offering for $reference.")
    } else {
        println("Failed to set dynamic offering for $reference.")
    }
}

// ---------- Getting Current Dynamic Offerings (Synchronous via Coroutine) ----------
// Using GlobalScope for demonstration purposes; 
// Consider using a proper coroutine scope in production code
GlobalScope.launch {
    val currentOfferings = Purchasely.getDynamicOfferings()
    if (currentOfferings.isEmpty()) {
        println("No dynamic offerings currently set.")
    } else {
        currentOfferings.forEach { offering ->
            println("- Ref: ${offering.reference}, Plan: ${offering.planId}, Offer: ${offering.offerId ?: "N/A"}")
        }
    }
}

// ---------- Getting Current Dynamic Offerings (Asynchronous) ----------
Purchasely.getDynamicOfferings { currentOfferings ->
    if (currentOfferings.isEmpty()) {
        println("No dynamic offerings currently set.")
    } else {
        currentOfferings.forEach { offering ->
            println("- Ref: ${offering.reference}, Plan: ${offering.planId}, Offer: ${offering.offerId ?: "N/A"}")
        }
    }
}

// ---------- Removing a Specific Dynamic Offering ----------
val removalReference = "dynamic_offering_reference"
Purchasely.removeDynamicOffering(removalReference)

// ---------- Clearing All Dynamic Offerings ----------
Purchasely.clearDynamicOfferings()