{% hint style="warning" %} In version 4 of the iOS SDK, this implementation was completed automatically.
The Experience Platform SDK will not automatically collect Lifecycle metrics. To continue collecting Lifecycle metrics, you must add code to your app. For more information, see Manual Lifecycle Implementation. {% endhint %}
Sessions contain information about the app's current lifecycle, such as the device information, the application install or upgrade information, the session start and pause times, the number of application launches, and additional context data that is provided by the developer through the LifecycleStart
API. Session data is persisted, so it is available across application launches.
{% tabs %} {% tab title="Android" %}
- Import the library:
import com.adobe.marketing.mobile.*;
{% endtab %}
{% tab title="iOS" %}
- Import the library:
#import "ACPLifecycle.h"
#import "ACPCore.h"
- In Swift, importing
ACPCore
also imports the necessary Lifecycle APIs:
import ACPCore
{% endtab %}
{% tab title="React Native" %}
Import the Lifecycle extension
import {ACPLifecycle} from '@adobe/react-native-acpcore';
{% endtab %}
{% tab title="Flutter" %}
Import the Lifecycle extension
import 'package:flutter_acpcore/flutter_acplifecycle.dart';
{% endtab %}
{% tab title="Cordova" %}
After creating your Cordova app and adding the Android and iOS platforms, the Lifecycle extension for Cordova can be added with this command:
cordova plugin add https://github.com/adobe/cordova-acpcore.git
{% endtab %}
{% tab title="Unity" %}
After importing the ACPCore.unitypackage, the Lifecycle extension for Unity can be added with following code in the MainScript
using com.adobe.marketing.mobile;
{% endtab %}
{% tab title="Xamarin" %}
After adding the iOS ACPCore NuGet package or the Android ACPLifecycle NuGet package, the Lifecycle extension can be added by this import statement
using Com.Adobe.Marketing.Mobile;
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Android" %}
-
Register the Lifecycle extension:
public class TargetApp extends Application { @Override public void onCreate() { super.onCreate(); MobileCore.setApplication(this); try { Lifecycle.registerExtension(); } catch (Exception e) { //Log the exception } } }
-
In the
onResume
function, start the lifecycle data collection:@Override public void onResume() { MobileCore.setApplication(getApplication()); MobileCore.lifecycleStart(null); }
Setting the application is only necessary on activities that are entry points for your application. However, setting the application on each Activity has no negative impact and ensures that the SDK always has the necessary reference to your application. We recommend that you call
setApplication
in each of your activities. -
In the
onPause
function, pause the lifecycle data collection:@Override public void onPause() { MobileCore.lifecyclePause(); }
To ensure accurate session and crash reporting, this call must be added to every activity. {% endtab %}
{% tab title="iOS" %}
-
Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // register the lifecycle extension [ACPLifecycle registerExtension]; return YES; }
-
Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theACPCore::start:
method in your app'sapplication:didFinishLaunchingWithOptions:
delegate method.If your iOS application supports background capabilities, your
application:didFinishLaunchingWithOptions:
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, thenlifecycleStart:
should only be called when the application state is not equal toUIApplicationStateBackground
.- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // register the lifecycle extension [ACPLifecycle registerExtension]; const UIApplicationState appState = application.applicationState; [ACPCore start:^{ // only start lifecycle if the application is not in the background if (appState != UIApplicationStateBackground) { [ACPCore lifecycleStart:nil]; } }]; }
-
When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:- (void) applicationWillEnterForeground:(UIApplication *)application { [ACPCore lifecycleStart:nil]; }
In iOS 13 and later, for a scene-based application, use the
UISceneDelegate
'ssceneWillEnterForeground
method as follows:- (void) sceneWillEnterForeground:(UIScene *)scene { [ACPCore lifecycleStart:nil]; }
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
-
When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:- (void) applicationDidEnterBackground:(UIApplication *)application { [ACPCore lifecyclePause]; }
In iOS 13 and later, for a scene-based application, use the
UISceneDelegate
'ssceneDidEnterBackground
method as follows:- (void) sceneDidEnterBackground:(UIScene *)scene { [ACPCore lifecyclePause]; }
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here
-
Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // register the lifecycle extension ACPLifecycle.registerExtension(); }
-
Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theACPCore::start:
method in your app'sapplication:didFinishLaunchingWithOptions:
delegate method.If your iOS application supports background capabilities, your
application:didFinishLaunchingWithOptions:
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, thenlifecycleStart:
should only be called when the application state is not equal toUIApplicationStateBackground
.func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // register the lifecycle extension ACPLifecycle.registerExtension(); let appState = application.applicationState; ACPCore.start { // only start lifecycle if the application is not in the background if appState != .background { ACPCore.lifecycleStart(nil) } } }
-
When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:func applicationWillEnterForeground(_ application: UIApplication) { ACPCore.lifecycleStart(nil) }
In iOS 13 and later, for a scene-based application, use the
UISceneDelegate
'ssceneWillEnterForeground
method as follows:func sceneWillEnterForeground(_ scene: UIScene) { ACPCore.lifecycleStart(nil) }
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
-
When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:func applicationDidEnterBackground(_ application: UIApplication) { ACPCore.lifecyclePause() }
In iOS 13 and later, for a scene-based application, use the
UISceneDelegate
'ssceneDidEnterBackground
method as follows:func sceneDidEnterBackground(_ scene: UIScene) { ACPCore.lifecyclePause() }
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here {% endtab %}
{% tab title="React Native" %} Registering the extension with Core:
When using React Native, registering Lifecycle with Mobile Core should be done in native code which is shown under the Android and iOS tabs.
Note: Implementing Lifecycle via JavaScript may lead to inaccurate Lifecycle metrics, therefore we recommend implementing Lifecycle in native Android and iOS code. However, these APIs are still provided in JavaScript to support flexible Lifecycle implementations.
Starting a lifecycle event:
ACPCore.lifecycleStart({"lifecycleStart": "myData"});
Pausing a lifecycle event:
ACPCore.lifecyclePause();
{% endtab %}
{% tab title="Cordova" %} When using Cordova, registering Lifecycle with Mobile Core must be done in native code which is shown under the Android and iOS tabs. {% endtab %}
{% tab title="Unity" %} Starting and Pausing a lifecycle event: Add the OnApplicationPause in the MainScript with the following code:
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
ACPCore.LifecyclePause();
}
else
{
var cdata = new Dictionary<string, string>();
cdata.Add("launch.data", "added");
ACPCore.LifecycleStart(cdata);
}
}
{% endtab %}
{% tab title="Xamarin" %} iOS
-
Register the Lifecycle extension with the SDK Core by adding the following to your app's
FinishedLaunching:
delegate method:public override bool FinishedLaunching(UIApplication app, NSDictionary options) { ACPLifecycle.RegisterExtension(); return base.FinishedLaunching(app, options); }
-
Start Lifecycle data collection by calling
LifecycleStart:
from within the callback of theACPCore::start:
method in your app'sFinishedLaunching:
delegate method.If your iOS application supports background capabilities, your
FinishedLaunching:
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, thenLifecycleStart:
should only be called when the application state is not equal toUIApplicationState.Background
.public override bool FinishedLaunching(UIApplication app, NSDictionary options) { ACPLifecycle.RegisterExtension(); // only start lifecycle if the application is not in the background var appstate = app.ApplicationState; if(appstate != UIApplicationState.Background) { ACPCore.LifecycleStart(null); } return base.FinishedLaunching(app, options); }
-
When launched, if your app is resuming from a backgrounded state, iOS might call your
WillEnterForeground:
delegate method. You also need to callLifecycleStart:
, but this time you do not need all of the supporting code that you used inFinishedLaunching:
:public override void WillEnterForeground(UIApplication uiApplication) { base.WillEnterForeground(uiApplication); ACPCore.LifecycleStart(null); }
-
When the app enters the background, pause Lifecycle data collection from your app's
DidEnterBackground:
delegate method:public override void DidEnterBackground(UIApplication uiApplication) { base.DidEnterBackground(uiApplication); ACPCore.LifecycleStart(null); }
Android
-
Register the Lifecycle extension:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); LoadApplication(new App()); ACPCore.Application = this.Application; ACPLifecycle.RegisterExtension(); }
-
In the
onResume
function, start the lifecycle data collection:protected override void OnResume() { base.OnResume(); ACPCore.LifecycleStart(null); }
Setting the application is only necessary on activities that are entry points for your application. However, setting the application on each Activity has no negative impact and ensures that the SDK always has the necessary reference to your application. We recommend that you set the application (
ACPCore.Application = this.Application;
) in each of your activities. -
In the
onPause
function, pause the lifecycle data collection:protected override void OnPause() { base.OnPause(); ACPCore.LifecyclePause(); }
To ensure accurate session and crash reporting, this call must be added to every activity. {% endtab %} {% endtabs %}
The following is a complete list of all of the metrics provided on your user's app lifecycle.
Metric | Key | Description |
---|---|---|
App ID | a.AppID | Stores the application name and version in the following format: AppName BundleVersion (app version code) .
An example of this format is MyAppName 1.1(1) . |
Device Name | a.DeviceName | Stores the device name. |
Operating System Version | a.OSVersion | Operating system name and version. |
Carrier Name | a.CarrierName |
Stores the name of the mobile service provider as provided by the device.
Important: This metric is not automatically stored in an Analytics variable. You must create a processing rule to copy this value to an Analytics variable for reporting. |
Resolution | a.Resolution | Width x Height in pixels. |
Locale | a.locale | Locale set for this device, for example, en-US. |
### Install | **Metric** | **Key** | **DescriptIon** | | :--- | :--- | :--- | | First Launches | `a.InstallEvent` | Triggered at the first run after installation or re-installation. | | Install Date | `a.InstallDate` | Date of first launch after installation. The format is `M/d/yyyy`, and an example is `5/3/2017`. | ### Upgrade | **Metric** | **Key** | **Description** | | :--- | :--- | :--- | | Upgrades | `a.UpgradeEvent` | Triggered at the first run after upgrade or when the version number changes. | | Days since last upgrade | `a.DaysSinceLastUpgrade` | Number of days since the application version number changed. | | Launches since last upgrade | `a.LaunchesSinceUpgrade` | Number of launches since the application version number changed. | ### Launch | **Metric** | **Key** | **Description** | | :--- | :--- | :--- | | Daily Engaged Users | `a.DailyEngUserEvent` | Triggered when the application is used on a particular day. **Important**: This metric is not automatically stored in an Analytics metric. You must create a processing rule that sets a custom event to capture this metric. | | Monthly Engaged Users | `a.MonthlyEngUserEvent` | Triggered when the application is used during a particular month. **Important**: This metric is not automatically stored in an Analytics metric. You must create a processing rule that sets a custom event to capture this metric. | | Launches | `a.LaunchEvent` | Triggered on every run, including crashes and installs. Also triggered when the app is resumed from the background after the lifecycle session timeout is exceeded. | | Previous Session Length | `a.PrevSessionLength` | Reports the number of seconds that a previous application session lasted based on how long the application was open and in the foreground. | | Ignored Session Length | `a.ignoredSessionLength` | If the last session is set to last longer than `lifecycle.sessionTimeout`, that session length is ignored and recorded here. | | Launch Number | `a.Launches` | Number of times the application was launched or brought out of the background. | | Days since first use | `a.DaysSinceFirstUse` | Number of days since first run. | | Days since last use | `a.DaysSinceLastUse` | Number of days since last use. | | Hour of Day | `a.HourOfDay` | Measures the hour the app was launched and uses the 24-hour numerical format. Used for time parting to determine peak usage times. | | Day of Week | `a.DayOfWeek` | Measures the day of the week the app was launched. | ### Crash | **Metric** | **Key** | **Description** | | :--- | :--- | :--- | | Crashes | `a.CrashEvent` | Triggered when the application crashed before closing. The event is sent when the application is started again after the crash. | ### Device Information
Metric | Key | Description |
---|---|---|
AppID | a.AppID
|
Stores the application name and version in the AppName BundleVersion (app version code) format.
An example of this format is MyAppName 1.1(1) . |
Device name | a.DeviceName
|
Stores the device name. |
Operating system version | a.OSVersion
|
Operating system name and version. |
Carrier name | a.CarrierName
|
Stores the name of the mobile service provider as provided by the devices. Important: This metric is not automatically stored in an Analytics variable. For reporting, you must create a processing rule to copy this value to an Analytics variable. |
Resolution | a.Resolution
|
Width x height, in pixels. |
Locale | a.Locale
|
Locale set for this device, for example, en-US. |
If you need to programmatically update your SDK configuration, use the following information to change your Lifecycle configuration values:
{% hint style="warning" %} The time that your app spends in the background is not included in the session length. {% endhint %}
Key | Description |
---|---|
lifecycle.sessionTimeout
|
Time, in seconds, that must elapse between the time the app is launched and before the launch is considered to be a new session. This timeout also applies when your application is sent to the background and reactivated. Default value is 300 seconds (5 minutes). |