---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# NowAnalyticsServiceDelegate protocol - iOS

# NowAnalyticsServiceDelegate protocol - iOS {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 5 minutes to read

The NowAnalyticsServiceDelegate protocol provides callback functions
that provide information about the starting and ending of a user session and screen change
detection.

## NowAnalyticsServiceDelegate - nowAnalyticsDidDetectScreen( _ sessionId: String) {#ariaid-title2}

Notifies when a screen change is detected. The screen name is detected from the
navigation bar. If that is not possible, it is the viewController subclass name.
{#NAnServDel-nowAnalDidDetectScreen_S__table_ljr_3bb_kqb__entry__3}

| Name | Type | Description |
|-|-|-|
| screenName | String | Name of the screen to detect whether there is any change. |
[Table 1. Parameters]

{#NAnServDel-nowAnalDidDetectScreen_S__table_ljr_3bb_kqb} {#NAnServDel-nowAnalDidDetectScreen_S__table_mjr_3bb_kqb__entry__2}

| Type | Description |
|-|-|
| String | Custom name to use to override the passed screenName. |
[Table 2. Returns]

{#NAnServDel-nowAnalDidDetectScreen_S__table_mjr_3bb_kqb}  
This example shows how to define a delegate class for
NowAnalyticsServiceDelegate.

    import NowAnalytics

    // Initialize the Analytics SDK
    NowAnalytics.configure(for: URL(string: "https://my.instance.service-now.com")!)

    // Define a delegate class for NowAnalyticsServiceDelegate
    class NowAnalyticsDelegate: NowAnalytics.NowAnalyticsServiceDelegate {
      func nowAnalyticsSessionShouldStart() -> Bool {
        // Session is about to start, return true to allow session to start
        return true
      }

      func nowAnalyticsSessionDidStart(_ sessionId: String) {
        // Session was started
      }

      func nowAnalyticsSessionShouldEnd(_ sessionId: String) -> Bool {
        // Session is about to end, return true to allow session to end
        return true
      }

      func nowAnalyticsSessionDidEnd(_ sessionId: String) {
        // Session was ended
      }

      func nowAnalyticsDidDetectScreen(_ screenName: String) -> String? {
        // Example of skipping specific screen detection
        if (screenName == "LoginViewController") {
          return nil
        }

        // Example of appending a prefix for every screen detected
        return "MyApp_" + screenName
      }
    }

    // Register delegate
    private var analyticsDelegate = NowAnalyticsDelegate()  // Keep ref of delegate
    NowAnalytics.sharedAnalyticsService.setDelegate(analyticsDelegate)

## NowAnalyticsServiceDelegate - nowAnalyticsSessionDidEnd( _ sessionId: String) {#ariaid-title3}

Notifies when
the
specified session has ended.
This is a callback function that gets called when the specified session actually ends.
{#NAnServDel-nowAnalSessionDidEnd_S__table_hnt_b2v_1qb__entry__3}

| Name | Type | Description |
|-|-|-|
| sessionId | String | Unique identifier of the session to check. |
[Table 3. Parameters]

{#NAnServDel-nowAnalSessionDidEnd_S__table_hnt_b2v_1qb} {#NAnServDel-nowAnalSessionDidEnd_S__table_int_b2v_1qb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 4. Returns]

{#NAnServDel-nowAnalSessionDidEnd_S__table_int_b2v_1qb}  
This example shows how to define a delegate class for
NowAnalyticsServiceDelegate.

    import NowAnalytics

    // Initialize the Analytics SDK
    NowAnalytics.configure(for: URL(string: "https://my.instance.service-now.com")!)

    // Define a delegate class for NowAnalyticsServiceDelegate
    class NowAnalyticsDelegate: NowAnalytics.NowAnalyticsServiceDelegate {
      func nowAnalyticsSessionShouldStart() -> Bool {
        // Session is about to start, return true to allow session to start
        return true
      }

      func nowAnalyticsSessionDidStart(_ sessionId: String) {
        // Session was started
      }

      func nowAnalyticsSessionShouldEnd(_ sessionId: String) -> Bool {
        // Session is about to end, return true to allow session to end
        return true
      }

      func nowAnalyticsSessionDidEnd(_ sessionId: String) {
        // Session was ended
      }

      func nowAnalyticsDidDetectScreen(_ screenName: String) -> String? {
        // Example of skipping specific screen detection
        if (screenName == "LoginViewController") {
          return nil
        }

        // Example of appending a prefix for every screen detected
        return "MyApp_" + screenName
      }
    }

    // Register delegate
    private var analyticsDelegate = NowAnalyticsDelegate()  // Keep ref of delegate
    NowAnalytics.sharedAnalyticsService.setDelegate(analyticsDelegate)

## NowAnalyticsServiceDelegate - nowAnalyticsSessionDidStart( _ sessionId: String) {#ariaid-title4}

Notifies when the specified session has started.
This is a callback function that gets called when the specified session actually
starts.
{#NAnServDel-nowAnalSessionDidStart_S__table_ntx_m2v_1qb__entry__3}

| Name | Type | Description |
|-|-|-|
| sessionId | String | Unique identifier of the session to check. |
[Table 5. Parameters]

{#NAnServDel-nowAnalSessionDidStart_S__table_ntx_m2v_1qb} {#NAnServDel-nowAnalSessionDidStart_S__table_otx_m2v_1qb__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates that the session is starting. Valid values: * true: Session is starting * false: Session is not starting |
[Table 6. Returns]

{#NAnServDel-nowAnalSessionDidStart_S__table_otx_m2v_1qb}  
This example shows how to define a delegate class for
NowAnalyticsServiceDelegate.

    import NowAnalytics

    // Initialize the Analytics SDK
    NowAnalytics.configure(for: URL(string: "https://my.instance.service-now.com")!)

    // Define a delegate class for NowAnalyticsServiceDelegate
    class NowAnalyticsDelegate: NowAnalytics.NowAnalyticsServiceDelegate {
      func nowAnalyticsSessionShouldStart() -> Bool {
        // Session is about to start, return true to allow session to start
        return true
      }

      func nowAnalyticsSessionDidStart(_ sessionId: String) {
        // Session was started
      }

      func nowAnalyticsSessionShouldEnd(_ sessionId: String) -> Bool {
        // Session is about to end, return true to allow session to end
        return true
      }

      func nowAnalyticsSessionDidEnd(_ sessionId: String) {
        // Session was ended
      }

      func nowAnalyticsDidDetectScreen(_ screenName: String) -> String? {
        // Example of skipping specific screen detection
        if (screenName == "LoginViewController") {
          return nil
        }

        // Example of appending a prefix for every screen detected
        return "MyApp_" + screenName
      }
    }

    // Register delegate
    private var analyticsDelegate = NowAnalyticsDelegate()  // Keep ref of delegate
    NowAnalytics.sharedAnalyticsService.setDelegate(analyticsDelegate)

## NowAnalyticsServiceDelegate - nowAnalyticsSessionShouldEnd( _ sessionId: String) {#ariaid-title5}

Notifies
when the specified session is about to end.
This is a callback function that gets called when the specified session is about to
end.
{#NAnServDel-nowAnalSessShouldEnd_S__table_lrd_pcv_1qb__entry__3}

| Name | Type | Description |
|-|-|-|
| sessionId | String | Unique identifier of the session to check. |
[Table 7. Parameters]

{#NAnServDel-nowAnalSessShouldEnd_S__table_lrd_pcv_1qb} {#NAnServDel-nowAnalSessShouldEnd_S__table_mrd_pcv_1qb__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates that the session is ending. Valid values: * true: Session is ending * false: Session is not ending |
[Table 8. Returns]

{#NAnServDel-nowAnalSessShouldEnd_S__table_mrd_pcv_1qb}  
This example shows how to define a delegate class for
NowAnalyticsServiceDelegate.

    import NowAnalytics

    // Initialize the Analytics SDK
    NowAnalytics.configure(for: URL(string: "https://my.instance.service-now.com")!)

    // Define a delegate class for NowAnalyticsServiceDelegate
    class NowAnalyticsDelegate: NowAnalytics.NowAnalyticsServiceDelegate {
      func nowAnalyticsSessionShouldStart() -> Bool {
        // Session is about to start, return true to allow session to start
        return true
      }

      func nowAnalyticsSessionDidStart(_ sessionId: String) {
        // Session was started
      }

      func nowAnalyticsSessionShouldEnd(_ sessionId: String) -> Bool {
        // Session is about to end, return true to allow session to end
        return true
      }

      func nowAnalyticsSessionDidEnd(_ sessionId: String) {
        // Session was ended
      }

      func nowAnalyticsDidDetectScreen(_ screenName: String) -> String? {
        // Example of skipping specific screen detection
        if (screenName == "LoginViewController") {
          return nil
        }

        // Example of appending a prefix for every screen detected
        return "MyApp_" + screenName
      }
    }

    // Register delegate
    private var analyticsDelegate = NowAnalyticsDelegate()  // Keep ref of delegate
    NowAnalytics.sharedAnalyticsService.setDelegate(analyticsDelegate)

## NowAnalyticsServiceDelegate - nowAnalyticsSessionShouldStart( _ sessionId: String) {#ariaid-title6}

Notifies whether the specified session is about to start.
This is a callback function that gets called when the specified session is about to
start.
{#NAnServDel-nowAnalSessShouldStart_S__table_ojw_z1v_1qb__entry__3}

| Name | Type | Description |
|-|-|-|
| sessionId | String | Unique identifier of the session to check. |
[Table 9. Parameters]

{#NAnServDel-nowAnalSessShouldStart_S__table_ojw_z1v_1qb} {#NAnServDel-nowAnalSessShouldStart_S__table_pjw_z1v_1qb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 10. Returns]

{#NAnServDel-nowAnalSessShouldStart_S__table_pjw_z1v_1qb}  
This example shows how to define a delegate class for
NowAnalyticsServiceDelegate.

    import NowAnalytics

    // Initialize the Analytics SDK
    NowAnalytics.configure(for: URL(string: "https://my.instance.service-now.com")!)

    // Define a delegate class for NowAnalyticsServiceDelegate
    class NowAnalyticsDelegate: NowAnalytics.NowAnalyticsServiceDelegate {
      func nowAnalyticsSessionShouldStart() -> Bool {
        // Session is about to start, return true to allow session to start
        return true
      }

      func nowAnalyticsSessionDidStart(_ sessionId: String) {
        // Session was started
      }

      func nowAnalyticsSessionShouldEnd(_ sessionId: String) -> Bool {
        // Session is about to end, return true to allow session to end
        return true
      }

      func nowAnalyticsSessionDidEnd(_ sessionId: String) {
        // Session was ended
      }

      func nowAnalyticsDidDetectScreen(_ screenName: String) -> String? {
        // Example of skipping specific screen detection
        if (screenName == "LoginViewController") {
          return nil
        }

        // Example of appending a prefix for every screen detected
        return "MyApp_" + screenName
      }
    }

    // Register delegate
    private var analyticsDelegate = NowAnalyticsDelegate()  // Keep ref of delegate
    NowAnalytics.sharedAnalyticsService.setDelegate(analyticsDelegate)


