API tracking for Cocoa apps
API Tracking is a part of Apptics' Analytics SDK which allows you to track the success rate and response time for the REST APIs configured in the Apptics web console. Refer to the User Guide to configure APIs in the web console.
Installation
Copiedsource 'https://github.com/CocoaPods/Specs.git'
target '[TARGET NAME]' do
pod 'AppticsApiTracker'
end
Import
- Once the configuration is done in the web console, you will get an API ID.
- Add the API ID in the header field of your API call. If the API ID is not added in the API header field, we match the incoming URLs with all the URLs that you have configured in the web console.
- For enabling interception, add Apptics between your (NS)URLSession and internet. Register your session configuration (NS)URLSessionConfiguration) to Apptics using (enableApiTrackingForSessionConfiguration).
Use the below configuration with (NS)URLSession
CopiedNSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[APAPIManager enableForSesionConfiguration:configuration];
Copiedlet configuration = URLSessionConfiguration.default
APAPIManager.enable(configuration)
- Use the above configuration with your (NS)URLSession.
CopiedNSURLSession *session = [[NSURLSession alloc] initWithConfiguration:configuration];
Copiedlet session = URLSession(configuration: configuration)
- If you are using any third-party frameworks like AFNetworking/Alamofire, pass the sessionConfiguration to the initializer of AFURLSessionManager/Manager.
CopiedObjective-C (AFNetworking)
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
Copiedlet manager = Alamofire.SessionManager(configuration: configuration)
Track with API ID
- Once the API configuration is done, include this key-value pair (API ID) in the header field of your HTTP connection. API-ID: <API-ID>
Track manually with API ID
- Use startTrackApi method with the API ID and the request method name (GET, POST, PUT, etc.) before making a network call. It will return a tracking ID. Pass that ID along with the API response code (200, 400, etc.) to the endTrackApimethod.
CopiedNSString *tracking_id = APAPIManager.startTrackingApi(<#T##API_ID: String##String#>, httpMethod: <#T##String#>)
- Call the endTrackApi method once the API has returned the response.
CopiedAPAPIManager.endTrackingApi(<#T##trackId: String##String#>, responseCode: <#T##String#>)