Avo Inspector iOS SDK

5 minute read

Quick Start Guide

Quick start guides are available in the GitHub repos:

Swift Package Manager guideCocoaPods guide

Installation

AvoInspector is available through CocoaPods. To install it, simply add the following line to your Podfile:

Ruby
Copy
1
pod 'AvoInspector'

The latest version can be found in GitHub releases tab.

Import

Swift
Copy
1
import AvoInspector
Objective C
Copy
1
#import <AvoInspector/AvoInspector.h>

Initialization

Obtain the API key in Inspector tab in your Avo.app workspace.

You will need to create an instance of AvoInspector with the constructor.

Swift
Copy
1
init(apiKey: String?, env: AvoInspectorEnv)
Objective C
Copy
1
-(instancetype) initWithApiKey: (NSString *) apiKey env: (AvoInspectorEnv) env;

Parameters:

  • String apiKey - the API key you get in Inspector tab of your Avo workspace
  • AvoInspectorEnv env - current environment: development, staging or production

Sending event schemas to Avo Inspector

This is the core of the Avo Inspector SDK.

Call **one of the methods** in this section every time an event is tracked.

Option 1

Swift
Copy
1
func trackSchema(fromEvent eventName: String?, eventParams params: [String : Any?]?) -> [String : AvoEventSchemaType]?
Objective C
Copy
1
-(NSDictionary<NSString *, AvoEventSchemaType *> *) trackSchemaFromEvent:(NSString *) eventName eventParams:(NSDictionary<NSString *, id> *) params;

Parameters:

  • eventName: String - event name, sometimes referred as event type.
  • eventProperties: NSDictionary<NSString *, id> * - event properties, which will be converted to event schema on the device and the event schema will be sent to Avo. Resulting keys will be the same and resulting values will be original value's types converted to schema types.
Example format:
Swift
Copy
1
2
3
4
5
let eventProperties: [String : Any] = [
"userId": 1337,
"emailAddress": "jane.doe@avo.app",
"key": "value"
]
Objective C
Copy
1
2
3
4
5
NSDictionary<NSString *, id> *eventProperties = @{
@"userId": @1337,
@"emailAddress": @"jane.doe@avo.app",
@"key": @"value"
};

Return Type:

  • NSDictionary<NSString *, AvoEventSchemaType *> * containing event schema, so you can verify that conversion was correct.
Example format:
Swift
Copy
1
2
3
4
5
let eventSchema: [String : AvoEventSchemaType] = [
"userId": AvoInt(),
"emailAddress": AvoString(),
"key": AvoString()
]
Objective C
Copy
1
2
3
4
5
NSDictionary<NSString _, AvoEventSchemaType _> \*eventSchema = @{
@"userId": [AvoInt new],
@"emailAddress": [AvoString new],
@"key": [AvoString new]
};

Option 2

Swift
Copy
1
func trackSchema(eventName: String, eventSchema: NSDictionary<String, AvoEventSchemaType>)
Objective C
Copy
1
-(void) trackSchema:(NSString *) eventName eventSchema:(NSDictionary<NSString *, AvoEventSchemaType *> *) schema;

This method allows you to process the event schema before sending it. It's handy to extract the schema from your event properties with extractSchema(eventProperties: NSDictionary<String, Any>) (see below), process it and then provide it to this method.

Parameters:

  • eventName: String - event name, sometimes referred as event type.
  • eventSchema: NSDictionary<String *, AvoEventSchemaType *> * - actual event schema that will be sent to Avo. Keys are event properties names and values are event properties types.
Example format:
Swift
Copy
1
2
3
4
5
let eventSchema: [String : AvoEventSchemaType] = [
"userId": AvoInt(),
"emailAddress": AvoString(),
"key": AvoString()
]
Objective C
Copy
1
2
3
4
5
NSDictionary<String _, AvoEventSchemaType _> \*eventSchema = @{
@"userId": [AvoInt new],
@"emailAddress": [AvoString new],
@"key": [AvoString new]
};

See this for details about event schema structure and schema types.

Other methods

1. Extract schema from event properties

Swift
Copy
1
func extractSchema(eventProperties: NSDictionary<String, Any>) -> NSDictionary<String, AvoEventSchemaType>
Objective C
Copy
1
-(NSDictionary<NSString _, AvoEventSchemaType _> _) extractSchema:(NSDictionary<NSString _, id> \*) eventParams;

This is the method used by trackSchemaFromEvent internally. It extracts the event schema in form of an NSDictionary from an event properties NSDictionary.

Parameters:

  • eventProperties: NSDictionary - event properties object. Keys are event properties names and values are event properties values.

Return Type:

  • NSDictionary<NSString *, AvoEventSchemaType *> * containing event schema of the given event properties. Keys are event properties names and values are event properties types. Example format:
Swift
Copy
1
2
3
4
5
6
let eventSchema: [String : AvoEventSchemaType] = [
"userId": AvoInt(),
"emailAddress": AvoString(),
"key": AvoString()
]
Objective C
Copy
1
2
3
4
5
NSDictionary<NSString _, AvoEventSchemaType _> \*eventSchema = @{
@"userId": [AvoInt new],
@"emailAddress": [AvoString new],
@"key": [AvoString new]
};

2. Print logs

Swift
Copy
1
func enableLogging(enable: Bool)
Objective C
Copy
1
+(void) enableLogging:(BOOL) enable;

This is a class method. enableLogging controls printing of tracked event schemas and other helpful information. Enabled by default in development environments.

Parameters:

  • enable: Bool - sets whether Avo Inspector SDK will print logs.

3. Control batching size

Swift
Copy
1
func setBatchSize(newBatchSize: Int32)
Objective C
Copy
1
+(void) setBatchSize:(int) newBatchSize;

This is a class method. Enables manual control over events batching. Default batch size in production is 30, i.e. the library attempts to send event schemas to the server when it has 30 or more schemas. In development batching is disabled by default.

Parameters:

  • newBatchSize: Int32 - sets batch size.

4. Control batching interval

Swift
Copy
1
func setBatchFlushSeconds(newBatchFlushSeconds: Int32)
Objective C
Copy
1
+(void) setBatchFlushSeconds: (int) newBatchFlushSeconds;

This is a class method. Enables manual control over events batching. Default production batch flush interval is 30 seconds, i.e. the library attempts to send event schemas to the server when 30 or more seconds pass, given there are unsent schemas.

Parameters:

  • newBatchFlushSeconds: Int32 - sets batch flush time in seconds.

Using the Visual Inspector

To use the Visual Inspector with Swift Package Manager you need to include it manually from https://github.com/avohq/ios-analytics-debugger-spm

Visual inspector is actually our Mobile Debugger integrated in Avo Inspector SDK.

Visual Inspector is enabled in development and staging environments by default.

Show

Swift
Copy
1
func show(type: AvoVisualInspectorType)
Objective C
Copy
1
- (void) showVisualInspector: (AvoVisualInspectorType) type;

Hide

Swift
Copy
1
func hideVisualInspector()
Objective C
Copy
1
- (void) hideVisualInspector;

Get instance of AnalyticsDebugger

Swift
Copy
1
func getVisualInspector(): AnalyticsDebugger?
Objective C
Copy
1
- (AnalyticsDebugger *) getVisualInspector;

See more about the AnalyticsDebugger in it's GitHub repo.

Auto inspection with Segment SDK

Here is a code snippet you can use to easily integrate with Segment. After registering this middleware every events sent to Segment will be automatically analyzed by Avo, i.e. you don't need to call trackSchemaFromEvent or trackSchema methods.

Swift
Copy
1
2
3
4
5
6
7
8
9
10
let config = SEGAnalyticsConfiguration(writeKey: "YOUR_WRITEKEY_HERE")
let avoMiddleware = SEGBlockMiddleware { (context, next) in
if let trackPayload = context.payload as? SEGTrackPayload {
avoInspector.trackSchema(fromEvent: trackPayload.event, eventParams: Dictionary(uniqueKeysWithValues:
trackPayload.properties?.map { key, value in (key.description, value) } ?? []))
}
next(context)
}
config.middlewares = [ avoMiddleware ]
SEGAnalytics.setup(with: config)
Objective C
Copy
1
2
3
4
5
6
7
8
9
10
11
SEGAnalyticsConfiguration * config = [SEGAnalyticsConfiguration configurationWithWriteKey: @"YOUR_WRITEKEY_HERE"];
SEGBlockMiddleware * avoMiddleware = [[SEGBlockMiddleware alloc] initWithBlock:^(SEGContext * _Nonnull context, SEGMiddlewareNext _Nonnull next) {
SEGPayload * payload = [context payload];
if ([payload isKindOfClass:[SEGTrackPayload class]]) {
SEGTrackPayload * trackPayload = (SEGTrackPayload *) payload;
[avoInspector trackSchemaFromEvent:[trackPayload event] eventParams:[trackPayload properties]];
}
next(context);
}];
config.middlewares = @[avoMiddleware];
[SEGAnalytics setupWithConfiguration:config];

See Segment iOS middleware docs for more information.