> For the complete documentation index, see [llms.txt](https://purchasely.gitbook.io/purchasely/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://purchasely.gitbook.io/purchasely/2.8/quick-start/observer-mode.md).

# Observer mode

Observer mode allows you to use Purchasely alongside your existing in-app subscription flow.

This is a great way to:

* Test Purchasely without changing your existing flow
* Receive the Server-to-server notifications to trigger your automations
* Connect our data with your marketing tools using our automations
* Analyse your business with our great charts
* and … smoothly migrate to Purchasely 🙂

### Configure the SDK

Please instantiate Purchasely as soon as the app starts so that we can grab the receipts presented on app launch (PSD2, ask-to-buy, …).

{% tabs %}
{% tab title="Swift" %}

```swift
import Purchasely

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
	Purchasely.start(withAPIKey: "API_KEY", appUserId: "USER_ID", observerMode: true)
	return true
}

```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
#import <Purchasely/Purchasely-Swift.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	// Override point for customization after application launch.

		[Purchasely startWithAPIKey:@"API_KEY"
					  appUserId:@"USER_ID"
				   observerMode:true
				  eventDelegate:nil
					 uiDelegate:nil
		 confirmPurchaseHandler:nil
					   logLevel: LogLevelInfo];

	return YES;
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
import io.purchasely.ext.Purchasely

Purchasely.Builder(applicationContext)
    .apiKey("API_KEY")
    .logLevel(LogLevel.DEBUG) // set to warning or error for release
    .userId("USER_ID")
    .eventListener(eventListener)
    .observerMode(true)
    .stores(listOf(GoogleStore(), HuaweiStore()))
    .build()

// When you are ready for Purchasely to initialize,
// you must call start() method that will grab configuration and products
// from the selected stores.
Purchasely.start()
```

{% endtab %}

{% tab title="Java" %}

```java
List<Store> stores = new ArrayList();
stores.add(new GoogleStore(), new HuaweiStore());

new Purchasely.Builder(getApplicationContext())
    .apiKey("API_KEY")
    .logLevel(LogLevel.DEBUG) // set to warning or error for release
    .userId("USER_ID")
    .eventListener(this)
    .observerMode(true)
    .stores(stores)
    .build();

// When you are ready for Purchasely to initialize,
// you must call start() method that will grab configuration and products
// from the selected stores.
Purchasely.start();
```

{% endtab %}

{% tab title="React Native" %}

```javascript
import Purchasely from 'react-native-purchasely';

Purchasely.startWithAPIKey(
  'API_KEY',
  ['Google'],
  'USER_ID',
  Purchasely.logLevelDebug,
  true
);

```

{% endtab %}

{% tab title="Cordova" %}

```javascript
Purchasely.startWithAPIKey('API_KEY', ['Google'], "USER_ID", Purchasely.LogLevel.ERROR, true);
```

{% endtab %}
{% endtabs %}

If your user change within your app life cycle, please update Purchasely like that:

{% tabs %}
{% tab title="Swift" %}

```swift
Purchasely.userLogin(with: "123456789")
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[Purchasely userLoginWith:@"123456789"];
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Purchasely.userLogin("123456789") { refresh ->
    if (refresh) {
        //you can call your backend to refresh user information
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
Purchasely.userLogin("123456789", refresh -> {
    if(refresh) {
        //you can call your backend to refresh user information
    }
    return null;
});
```

{% endtab %}

{% tab title="React Native" %}

```javascript
Purchasely.userLogin('123456789').then((refresh) => {
  if (refresh) {
    //call your backend to refresh user information
  }
});
```

{% endtab %}

{% tab title="Cordova" %}

```javascript
Purchasely.userLogin("123456789");
```

{% endtab %}
{% endtabs %}

When a user logs out perform a:

{% tabs %}
{% tab title="Swift" %}

```swift
Purchasely.userLogout()
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[Purchasely userLogout];
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Purchasely.userLogout()
```

{% endtab %}

{% tab title="Java" %}

```java
Purchasely.userLogout();
```

{% endtab %}

{% tab title="React Native" %}

```javascript
Purchasely.userLogout();
```

{% endtab %}

{% tab title="Cordova" %}

```javascript
Purchasely.userLogout();
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
Purchasely won't consume your purchases or acknowledge purchases made.

On **iOS** we won't finish the transaction of your consumables that will **remain in the queue** if you don't do that in your code.

On **Android** the transactions will be cancelled and **refunded after 3 days**.
{% endhint %}

### Sync your purchases (Android only)

When a purchase or a restoration is made with your current flow, call the `synchronize()` method of our SDK to send the receipt to our backend. This allow us to save the receipts on our server to prepare for your migration.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
Purchasely.synchronize()
```

{% endtab %}

{% tab title="Java" %}

```java
Purchasely.synchronize();
```

{% endtab %}

{% tab title="React Native" %}

```javascript
Purchasely.synchronize();
```

{% endtab %}

{% tab title="Cordova" %}

```javascript
Purchasely.synchronize();
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://purchasely.gitbook.io/purchasely/2.8/quick-start/observer-mode.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
