For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.
Instrumenting your app to capture custom analytics
Custom analytics can be captured by using the WLAnalytics API. The MobileFirst Analytics API provides a log method with which arbitrary key/value pairs can be recorded and sent to the MobileFirst Analytics Server.
Custom analytics can include any data you collect. For example:
- Page flow/transitions
- Recording gestures
- Recording button clicks
As an example to collect page transitions, follow these steps.
- On iOS, add the following code in the ViewController's viewDidLoad method.
NSArray *viewControllers = self.navigationController.viewControllers; NSUInteger numViewControllers = viewControllers.count; if (numViewControllers >= 2) { UIViewController *previous = [viewControllers objectAtIndex:(numViewControllers - 2)]; NSDictionary *metadata = @{@"fromPage": previous.title, @"toPage": self.title}; [[WLAnalytics sharedInstance] log:@"page transition" withMetadata:metadata]; }
On Android, add the following code in an Activity subclass' onResume() method. String fromActivity = getIntent().getStringExtra("fromActivity"); JSONObject metadata = new JSONObject(); metadata.put("fromPage", fromActivity); metadata.put("toPage", "This Page"); WLAnalytics.log("page transition", metadata);
In JavaScript (Cordova), add the following code. // before showing the element identified by 'setting' $(document).on('pagebeforeshow', '#setting', function(e, obj) { WL.Analytics.log('page transition', { 'fromPage': obj.prevPage[0].id, 'toPage': obj.toPage[0].id }); });
For the web API, custom data is sent by the addEvent method. webAnalytics.addEvent({'Purchases':'radio'}); webAnalytics.addEvent({'src':'App landing page','target':'About page'});
What to do next
As with all analytic data that is collected from the mobile app, the send() method must be called for the data to appear in the MobileFirst Analytics Console.
Parent topic: Custom analytics