Concepts / Building Search UI / Plug analytics
Jan. 07, 2019

Plug analytics

Using Algolia Insights with iOS InstantSearch

Introduction

InstantSearch Insights iOS library allows developers to capture search-related events. The events maybe related to search queries (such as click an conversion events used for Click Analytics or A/B testing). It does so by correlating events with queryIDs generated by the search API when a query parameter clickAnalytics=true is set. As well library allows to capture search-independent events which can be used for the purpose of search experience personalisation. There are three types of these events which are currently supported: click, conversion and view.

Supported platforms

InstantSearch Insights iOS is supported on iOS, macOS, tvOS and watchOS, and is usable from both Swift and Objective-C.

Install

  1. Add a dependency on InstantSearchInsights:
    • CocoaPods: add pod 'InstantSearchInsights', '~> 2.0.0-beta' to your Podfile.
    • Carthage: add github "algolia/instantsearch-ios-insights" ~> 2.0.0-beta to your Cartfile.
  2. Add import InstantSearchInsights to your source files.

Initialize the Insights client

You first need to initialize the Insights client. For that you need your Application ID and API Key. You can find them on your Algolia account. Also, for the purpose of personalisation an application User Token can be specified via the correspponding optional parameter. In case of non-specified user token an automatically-generated application-wide user token will be used.

1
2
// Swift
Insights.register(appId: "testApp", apiKey: "testKey", userToken: "testToken")
1
2
3
4
// ObjC
[Insights registerWithAppId:@"testApp"
                     apiKey:@"testKey"
                  userToken:@"testToken"];

Sending metrics

Once you have registered your index with the Application ID and the API Key, you can easily start sending metrics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Swift
Insights.shared?.clickedAfterSearch(eventName: "click event",
                                    indexName: "index name",
                                    objectID: "object id",
                                    position: 1,
                                    queryID: "query id")

Insights.shared?.convertedAfterSearch(eventName: "conversion event",
                                      indexName: "index name",
                                      objectIDs: ["obj1", "obj2"],
                                      queryID: "query id")

Insights.shared?.viewed(eventName: "view event",
                        indexName: "index name",
                        filters: ["brand:apple"])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ObjC	
[[Insights shared] clickedAfterSearchWithEventName:@"click event"
                                         indexName:@"index name"
                                          objectID:@"object id"
                                          position:1
                                           queryID:@"query id"
                                         userToken:nil];

[[Insights shared] convertedAfterSearchWithEventName:@"conversion event"
                                           indexName:@"index name"
                                            objectID:@"object id"
                                             queryID:@"query id"
                                           userToken:nil];

[[Insights shared] viewedWithEventName:@"view event"
                             indexName:@"index name"
                               filters:@[@"brand:apple"]
                             userToken:nil];

Logging and debugging

In case you want to check if the metric was sent correctly, you need to enable the logging first

1
2
// Swift
Insights.shared(appId: "appId")?.isLoggingEnabled = true

After you enabled it, you can check the output for success messages or errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Success
[Algolia Insights - appName] Sync succeded for EventsPackage(id: "37E9A093-8F86-4049-9937-23E99E4E4B33", events: [{
    eventName = "search result click";
    eventType = click;
    index = "my index";
    objectIDs =     (
        1234567
    );
    positions =     (
        3
    );
    queryID = 08a76asda34fl30b7d06b7aa19a9e0;
    timestamp = 1545069313405;
    userToken = "C1D1322E-8CBF-432F-9875-BE3B5AFDA498";
}], region: nil)

//Error
[Algolia Insights - appName] The objectID field is missing (Code: 422)

Events flush delay

By default the client transmits tracked events every 30 minutes. You can customize this delay by changing flushDelay value (in seconds) as follows:

1
2
// Swift
Insights.flushDelay = 60
1
2
// ObjC
[Insights setFlushDelay: 60];	

Setting API region

By default each analytics API call is geo-routed so that each call targets the closest API. Today the analytics API suports two regions: United States and Germany. You can specify the region your prefer to use as follows:

1
2
// Swift
Insights.region = .de
1
2
// ObjC
[Insights setRegion: [Region de]]];

Usage

Check InstantSearch Insights for iOS and the general Click Analytics Overview.

To get a more meaningful search experience, please follow our Getting Started Guide.

Getting Help

Getting involved

  • If you want to contribute please feel free to submit pull requests.
  • If you have a feature request please open an issue.
  • If you use InstantSearch in your app, we would love to hear about it! Drop us a line on discourse or twitter.

Did you find this page helpful?