# Examples

Copy ready-made code for the most common Screeb tasks, from setup and identity to events, surveys, hooks and logout.

Each example below covers one common task. They assume the SDK is installed as shown in [Install](https://developers.screeb.app/sdk-ios/install). Replace the placeholders with your own values.

For a complete working app, open the example for your SDK in the [examples folder of the Screeb SDK repository](https://github.com/ScreebApp/sdk/tree/master/examples).

## Initialize with a logged-in user

When the user is already logged in at startup, pass their id and properties to the initialization. Screeb then ties the session to that user from the start, with no anonymous user in between.

```swift
Screeb.initSdk(
    context: nil,
    channelId: "<YOUR-CHANNEL-ID>",
    identity: "<USER-ID>",
    visitorProperty: [
        "email": "jane@example.com",
        "plan": "pro",
    ]
)
```

## Identify the user after login

When the app starts before the user logs in, initialize Screeb without a user, then identify them once they log in. Screeb keeps the activity of the anonymous session and attaches it to that user. See [Identity](https://developers.screeb.app/sdk-ios/identity) for the details.

```swift
Screeb.setIdentity(
    uniqueVisitorId: "<USER-ID>",
    visitorProperty: [
        "email": "jane@example.com",
        "plan": "pro",
    ]
)
```

## Track an event

Track what the user does, with properties that describe it. You can then target surveys at users who did it. See [Event tracking](https://developers.screeb.app/sdk-ios/event-tracking).

```swift
Screeb.trackEvent(
    name: "plan_upgraded",
    trackingEventProperties: [
        "plan": "pro",
        "seats": 5,
    ]
)
```

## Track a screen

Report each screen the user opens, so you can target surveys at a screen. See [Screen tracking](https://developers.screeb.app/sdk-ios/screen-tracking).

```swift
Screeb.trackScreen(name: "Checkout")
```

## Assign the user to a group

Put the user in a group, such as their company, with properties that describe the group. See [Group assignation](https://developers.screeb.app/sdk-ios/group-assignation).

```swift
Screeb.assignGroup(
    type: "company",
    name: "Acme",
    properties: ["plan": "enterprise"]
)
```

## Start a survey with hidden fields

Start a survey from your code, for example once a checkout completes, and pass hidden fields that Screeb saves with the response. See [Start a survey programmatically](https://developers.screeb.app/sdk-ios/start-survey-programmatically) for the other options.

```swift
Screeb.startSurvey(
    surveyId: "<SURVEY-ID>",
    hiddenFields: [
        "source": "checkout",
        "cart_value": 149.99,
    ]
)
```

## Run code when a survey is completed

Pass hooks to the initialization to run your code on Screeb events. This one logs each completed survey. Hooks always need a `version`. See [Hooks](https://developers.screeb.app/sdk-ios/hooks) for every hook and its payload.

```swift
Screeb.initSdk(
    context: nil,
    channelId: "<YOUR-CHANNEL-ID>",
    hooks: [
        "version": "1.0.0",
        "onSurveyCompleted": { (payload: Any) -> () in
            print("Survey completed", payload)
        },
    ]
)
```

## Reset the identity on logout

When the user logs out, reset the identity. Screeb starts a new anonymous session, so the next person on the device is not mixed with the previous user.

```swift
Screeb.resetIdentity()
```
