User Subscriptions History

This section provides details about the history of user subscriptions

Overview

The Purchasely SDK provides the ability to retrieve the list of expired subscriptions for a user, which constitutes the user's subscriptions history, directly from your app without calling your backend.
This feature is particularly useful for understanding user behavior, analyzing subscription trends, and enhancing user engagement strategies.

🚧

Minimum SDK versions

  • iOS: 4.4.0
  • Android: 4.4.0
  • Flutter: 4.4.0
  • ReactNative: 4.4.0
  • Cordova: 4.4.0
  • Unity: 4.4.0

Implementation

To retrieve the subscriptions history, use the appropriate code snippet for your technology.

Purchasely.userSubscriptionsHistory(success: { (subscriptions) in
	// Subscription object contains the plan purchased and the source it was purchased from (iOS or Android)
	// Calling unsubscribe() will either switch the user to its AppStore settings 
	// or display a procedure on how to unsubscribe on Android
}, failure: { (error) in
	// Display error
})
Purchasely.userSubscriptionsHistory(
    onSuccess = { list ->
        // Subscription object contains the plan purchased and the source it was purchased from (iOS or Android)
        // Calling unsubscribe() will either switch the user to its Google Play settings
        // or display a procedure on how to unsubscribe on iOS
    },
    onError = { throwable ->
        //Display error
    }
)
try {
  const subscriptions = await Purchasely.userSubscriptionsHistory();
  console.log(' ==> Subscriptions history');
  if (subscriptions[0] !== undefined) {
    console.log(subscriptions[0].plan);
    console.log(subscriptions[0].subscriptionSource);
    console.log(subscriptions[0].nextRenewalDate);
    console.log(subscriptions[0].cancelledDate);
  }
} catch (e) {
  console.log(e);
}
try {
  List<PLYSubscription> subscriptions =
      await Purchasely.userSubscriptionsHistory();
  print(' ==> Subscriptions history');
  if (subscriptions.isNotEmpty) {
    print(sdaubscriptions.first.plan);
    print(subscriptions.first.subscriptionSource);
    print(subscriptions.first.nextRenewalDate);
    print(subscriptions.first.cancelledDate);
  }
} catch (e) {
  print(e);
}
private PurchaselyRuntime.Purchasely _purchasely;

...
_purchasely.GetUserSubscriptionsHistory(OnGetSubscriptionsHistorySuccess, Log);
...

private void OnGetSubscriptionsHistorySuccess(List<SubscriptionData> subscriptionData)
{
  Log("Get Subscriptions history succeed.");

  foreach (var subscription in subscriptionData)
  {
    Log($"Subscription ID: {subscription.id}");

    var plan = subscription.plan;
    if (plan != null)
      LogPlan(plan);

    var product = subscription.product;
    if (product != null)
      LogProduct(product);
  }
}
Purchasely.userSubscriptionsHistory(subscriptions => {
       console.log("Subscriptions history" + subscriptions);
		}, (error) => {
		   console.log(error);
		}
);

By implementing the above code, you will retrieve an array of Subscriptions.

public class PLYSubscription: NSObject {
	
  public var product: PLYProduct
  public var plan: PLYPlan
  public var subscriptionSource: PLYSubscriptionSource
  public var nextRenewalDate: Date?
  public var cancelledDate: Date?
  public var originalPurchasedDate: Date?
  public var purchasedDate: Date?
  public var offerType: PLYSubscriptionOfferType
  public var status: PLYSubscriptionStatus
  public var environment: PLYSubscriptionEnvironment
  public var storeCountry: String?
  public var isFamilyShared: Bool
  public var contentId: String?
  public var offerIdentifier: String?

}
class PLYSubscriptionData(
    val data: PLYSubscription,
    val plan: PLYPlan,
    val product: PLYProduct
)

class PLYSubscription(
    val id: String? = null,
    val storeType: StoreType? = null,
    val purchaseToken: String? = null,
    val planId: String? = null,
    val cancelledAt: String? = null,
    val nextRenewalAt: String? = null,
    val originalPurchasedAt: String? = null,
    val purchasedAt: String? = null,
    val offerType: PLYOfferType? = PLYOfferType.NONE,
    val environment: PLYEnvironment? = null,
    val storeCountry: String? = null,
    val isFamilyShared: Boolean? = null,
    val contentId: String? = null,
    val offerIdentifier: String? = null,
    val subscriptionStatus: PLYSubscriptionStatus? = null,
    val cumulatedRevenuesInUSD: Double? = null,
    val subscriptionDurationInDays: Int? = null,
    val subscriptionDurationInWeeks: Int? = null,
    val subscriptionDurationInMonths: Int? = null,
)
type PurchaselySubscription = {
  purchaseToken: string;
  subscriptionSource: SubscriptionSource;
  nextRenewalDate: string;
  cancelledDate: string;
  plan: PurchaselyPlan;
  product: PurchaselyProduct;
};
class PLYSubscription {
  String? purchaseToken;
  PLYSubscriptionSource? subscriptionSource;
  String? nextRenewalDate;
  String? cancelledDate;
  PLYPlan? plan;
  PLYProduct? product;
}
public class SubscriptionData
{
  public Plan plan;
  public Product product;
  public string contentId;
  public string environment;
  public string id;
  public bool isFamilyShared;
  public string offerIdentifier;
  public SubscriptionOfferType offerType;
  public string originalPurchasedAt;
  public string purchaseToken;
  public string purchasedDate;
  public string nextRenewalDate;
  public string cancelledDate;
  public string storeCountry;
  public StoreType storeType;
  public SubscriptionStatus status;
}
subscription.id;
subscription.storeType;
subscription.purchaseToken;
subscription.planId;
subscription.cancelledAt;
subscription.nextRenewalAt;
subscription.originalPurchasedAt;
subscription.purchasedAt;
subscription.plans
subscription.offerType;
subscription.environment;
subscription.storeCountry;
subscription.isFamilyShared;
subscription.contentId;
subscription.offerIdentifier;
subscription.subscriptionStatus;
subscription.cumulatedRevenuesInUSD;
subscription.subscriptionDurationInDays;
subscription.subscriptionDurationInWeeks;
subscription.subscriptionDurationInMonths;

This list of Expired Subscriptions is the one the SDK leverages to fill in the Built-in Expired Sub. Attributes