Android SDK Integration Guide

Notice

1. Currently, SDK is in a beta version (1.1.4).
2. SDK 1.1.4 Change log - 웹뷰 내 오브젝트 추적 기능 추가 (Beta)

- recyclerview-v7 라이브러리 의존성 확인
- 서비스 안정성 향상
- 기타 버그 수정 
3. If you want to avoid the collection of sensitive information, use the function Blinding Sensitive Information.

1. Import Userhabit SDK

1) Download Userhabit Android SDK.

2) Decompress the file and add the userhabit_x.x.x.jar file into the "libs" folder under the Android project folder.

3) Add the userhabit_x.x.x.jar file to Java Build Path.

Add the following to your build.gradle file:

repositories {
  maven { url 'https://repo.userhabit.io/content/groups/public'}
}
dependencies {
  compile 'io.userhabit.service:userhabitsdk:0.2.5'
}       

2. Add Uses Permission

The following permission is required to transmit the data of Userhabit.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Check if the above permission is added to AndroidManifest.xml.

3. Add API Key

1) Copy the API Key, provided after creating an application on the Userhabit console page.

Example: 7f46775a5561194b3d3ebc046ddedd5dc8b2

2) Insert the following code in the part below (inside <application>) in the AndroidManifest.xml file. Enter the copied API Key in the “YOUR KEY” section of android:value.

<meta-data android:name="userhabitApiKey" android:value="YOUR KEY" />

4. Add Service

Insert the following code below (inside <application>) in the AndroidManifest.xml file.

<service android:name="io.userhabit.service.main.service.UserhabitService" />

5. Insert Source Code

Lastly, insert Userhabit.activityStart(this)Userhabit.activityStop(this) in the onStart(), onStop() method of all Activities of the application respectively.

Tip If the activity inherits a parent (Super) Activity (not android.app.Activity), just insert the code in the onStart(), onStop() method in the parent (Super) Activity. Then, you don’t have to additionally insert the code in all child activities.

Example

public class MainActivity extends Activity {

    ...

    @Override
    protected void onStart() {

        super.onStart();
        Userhabit.activityStart(this);

        // Your code here

    }
    @Override
    protected void onStop() {

        super.onStop();
        Userhabit.activityStop(this);

        // Your code here
    }

    ...

}

Tip In case all activities inherit super Activity

public class SuperActivity extends Activity {

    ...

    @Override
    protected void onStart() {

        super.onStart();
        Userhabit.activityStart(this);

        // Your code here

    }
    @Override
    protected void onStop() {

        super.onStop();
        Userhabit.activityStop(this);

        // Your code here
    }

    ...
}

public class MainActivity extends SuperActivity{

    // No need to add anything
}

public class TestActivity extends SuperActivity{

    // No need to add anything
}

Congratulations!

Now you have just completed the basic integration. Upon the completion of the basic integration, Screen Analysis is automatically initiated.

For more information on how to apply a variety of functions, please check the details in Additional Setting tab.

Android Additional Setting Guide


Dynamic Insertion of API Key without Using Manifest

As a value of meta data, Manifest cannot be revised in run time, the above method does not allow you to conduct the A/B test by dynamically changing the API key or to put a different setting on the API Key in the development/production environment.

To resolve the issue, remove the Metadata from Manifest file (described in section 3), and use the following method.

public class MainActivity extends Activity {

...

@Override
protected void onStart() {

    super.onStart();
      Userhabit.activityStart(this, "YOUR-API-KEY!!");

    // Your code here

}
@Override
protected void onStop() {

    super.onStop();
    Userhabit.activityStop(this);

    // Your code here
}

...

}

Analyzing ViewPager UI

Sdk android viewpager01Sdk android viewpager02

If you are using ViewPager provided from android.support.v4, one activity is consist of multiple screens. (Note, the picture above)

To analyze such UI, use the following method.

You need v4 support library v.22.2.0 and up in order to use this function.

// Insert the following code in the Activity that uses ViewPager.
protected void onStart() {
  super.onStart();

  // Use this after activityStart method
  Userhabit.activityStart(this);
  Userhabit.setViewPager(mViewPager);

// The name of screens are automatically generated by the function, using the name of pagerAdapter.
}
  

Analyzing Multiple Screens in a Single Activity

Sdk android activity screens

When an application is implemented, multiple screens(fragments) are used in one activity, or the pop-up menu appears within an activity, such as Fragment or  Navigation Drawer, is used.

In this case, you can use setScreen method to set user-defined screen and conduct more accurate screen analysis.

// Call following method to set user-defined screen
Userhabit.setScreen("User-defined Screen Name");
// Tab UI Example
void openTab1(){
    Userhabit.setScreen("tab1");

    // Your code here
}
void openTab2(){
    Userhabit.setScreen("tab2");

    // Your code here
}
// Navigation Drawer Example
public void onDrawerOpened(View drawerView) {
    Userhabit.setScreen("navigationMenuAppeared");

    // Your code here
    getActionBar().setTitle(mDrawerTitle);
}

In case of a switch to another screen within a single activity as shown in the above example, SDK is notified of the switch through the setScreen method, and takes a screenshot of the relevant screen and separately collects touch data.


Capture Screenshot Manually

You can take a screenshot manually. By default, the screenshot is automatically captured when an activity starts.

With the following method, you can capture the screenshot when specific UI is visible.

public void showMenuUI() {

// Your code here

// In order to capture a clear screenshot, use this method after all UI components are loaded.
Userhabit.takeScreenshot();
}

*If takeScreenshot() method is not used, SDK will automatically capture the screenshot when an activity starts.


Set the Session End Time

A session is defined as follows.

From the start of application to the end of application, or 10 seconds after the application goes to the background

(By default, a session is closed 10 seconds after the app stops or goes to the background.)

Other analytics that use the concept of session generally define a session for 10 seconds (Google Analytics uses 20 seconds.)

Userhabit.setSessionEndTime(int second)

Example

@Override
protected void onStart() {
    super.onStart();

    // Implement after the execution of the first activityStart function
    Userhabit.activityStart(this);

    // When the application is closed or goes to the background, a session is closed 15 seconds later.
    // Execute only once.
    Userhabit.setSessionEndTime(15);

    // If the session end time is too long, transmitting the data to the server will be delayed.

    // Your code here
    }

Cover Sensitive Information

Userhabit provides a method of blinding sensitive information of the end users. Use the following method in UIView in case there is some information you want to hide. You can use it after executing the method of  "Userhabit.activityStart();".

// You can pass the View object to hide as an argument.
// Userhabit.makeViewSensitive(View view)

// Or you can pass the view ID to hide as an argument.
// Userhabit.makeViewSensitive(int id)

@Override
protected void onStart() {
    super.onStart();

    // Use makeViewSensitive() after activityStart() method.
    Userhabit.activityStart(this);

    // Pass the View object or view ID as an argument.
    Userhabit.makeViewSensitive(R.id.sensitiveLayout);

    // Your code here
}
Objective-C
Swift

iOS SDK Integration Guide

Notice

1. Currently, SDK is in a beta version (1.0.18).
2. SDK 1.0.18 Change log

- 수동 종료 기능 추가
- 오브젝트 아이디 기능 개선
- 오브젝트 아이디 수동 설정 가능
3. If you want to avoid the collection of sensitive information, use the function Blinding Sensitive Information.

1. Import Userhabit SDK

1) Download Userhabit iOS SDK.

2) Decompress the file and add the iOS framework into the project.
Import all of the followings: CFNetwork.framework / MobileCoreService.framework / SystemConfiguration.framework / libsqlite3.dylib / libz.dylib

Sdk guide ios

Import Userhabit framework into Bridge-Header.h.

Sdk guide ios 2

Add pod 'Userhabit' to your Podfile and run pod install.

2. Add API Key

1) Copy the API Key, provided after creating an application on the Userhabit console page.

Example: 7f46775a5561194b3d3ebc046ddedd5dc8b2

2) Insert the API Key in the AppDelegate.m.

#import “AppDelegate.h"
#import <Userhabit/Userhabit.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [Userhabit SessionStart:@"Your API-KEY"];
    return YES;
}
import UIKit
import Userhabit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
           
Userhabit.SessionStart("Your API-Key");
        
        self.window!.makeKeyAndVisible()
        return true
    }
    ...
    ...
    ...
}

Warning!

When xib is not used
[Userhabit SessionStart:@”Your API-Key”];   Use this method after allocating self.window
When you see "file not found", after #import <Userhabit/Userhabit.h>
Please add "Userhabit.framework" file path to "Target -> Build Setting -> Frameworks Search Paths"
When xib is not used
Userhabit.SessionStart("Your API-Key");   Use this method after allocating self.window
When you see "file not found", after #import <Userhabit/Userhabit.h>

Please add "Userhabit.framework" file path to "Target -> Build Setting -> Frameworks Search Paths"

Insert "#import <Userhabit/Userhabit.h>" into "ProjectName-Bridging-Header.h"

Congratulations!

Now you have just completed the basic integration. Upon the completion of the basic integration, Screen Analysis is automatically initiated.

For more information on how to apply a variety of functions, please check the details in Additional Setting tab.

Objective-C
Swift

iOS Additional Setting Guide


Analyzing Multiple Screens in a Single UIViewController

When an application is implemented, sometimes multiple screens are appears within an single UIViewController.

In this case, you can set custome-defined screen using code set below to conduct more accurate screen analysis.

//Method Definition
[Userhabit setScreen:(id)sender withName:(NSString *)screenName];
//Method Definition 
setScreen(sender: AnyObject!, withName subViewName: String!)
//Add the function UIViewController, UIView in the place you want to define a new screen.
[Userhabit setScreen:self withName:@”User-defined String”];
//Add the function UIViewController, UIView in the place you want to define a new screen.
Userhabit.setScreen(self, withName:@”User-defined String”)
//Example of using Tab UI

-(void)reloadView:(UIButton *)touchBtn
{
    if (touchBtn.tag == 0) {
        [Userhabit setScreen:self withName:@"tab1"];
    } else if (touchBtn.tag == 1){
        [Userhabit setScreen:self withName:@"tab2"];
    }
}
//When a screen is assigned automatically while loading the screen,
//You can call a setScreen method choosing a place among three methods below.
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [Userhabit setScreen:self withName:@"tab1"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    ...
    ...
    ...
    [Userhabit setScreen:self withName:@"tab1"];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [Userhabit setScreen:self withName:@"tab1"];
}

In case of a switch to another screen within an UIViewController, UIView as shown in the above example, SDK is notified of the switch through the setScreen method, and takes a screenshot of the relevant screen and separately collects touch data.


Capture Screenshot Manually

You can take a screenshot manually. By default, the screenshot is automatically captured when an activity starts.

With the following method, you can capture the screenshot when specific UI is visible.

-(void)completeLoadData
{
    //Your code here
    // Please use the method after all UI loaded.
    // If you use setScreen method in viewWillAppear, please use the mothod below.
    [Userhabit takeScreenShot:self];
}
func loadData(){
         //Your code here
         // Please use the method after all UI loaded.
        // If you use setScreen method in viewWillAppear, please use the mothod below.
        Userhabit.takeScreenShot(self)
}

Set the Session End Time

A session is defined as follows.

From the start of application to the end of application, or 10 seconds after the application goes to the background

(By default, a session is closed 10 seconds after the app stops or goes to the background.)

Other analytics that use the concept of session generally define a session for 10 seconds (Google Analytics uses 20 seconds.)

[Userhabit setSessionDelayTime:Your time(float)];
//Method Definition 
setSessionDelayTime(delayInterval: Float)

Example

#import "AppDelegate.h"
#import <Userhabit/Userhabit.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [Userhabit SessionStart:@"Your API-KEY"];
    // When the application is closed or goes to the background, a session is closed 15 seconds later.
    // Execute only once.
    [Userhabit setSessionDelayTime:15.0f];

    return YES;
}
import UIKit
import Userhabit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        Userhabit.SessionStart("Your API-Key");
        Userhabit.setSessionDelayTime(15.0)
        self.window!.makeKeyAndVisible()
        return true
    }
    ...
    ...
    ...
}

Cover Sensitive Information

Userhabit provides a method of blinding sensitive information of the end users. Use the following method in UIViewController in case there is some information you want to hide.
Userhabit considers UITextField, UITextView as a sensitive information so that cover that field with black mask on the screen.

//You can hide the contents of the view to protect sensitive information.
//[Userhabit makeViewSensitive:(id)sender];

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UILabel *sensitiveLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 20, 30)];
    sensitiveLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:sensitiveLabel];
    
    [Userhabit makeViewSensitive:sensitiveLabel];
    
}
//Method Definition 
makeViewSensitive(sender: AnyObject!)
Override func viewDidLoad(){
        var pwLabel = UILabel(frame: CGRectMake(20, 150, 60, 30))
        pwLabel.backgroundColor = UIColor.clearColor()
        pwLabel.text = "PW"
        pwLabel.textAlignment = NSTextAlignment.Center
        self.view.addSubview(pwLabel)
        Userhabit.makeViewSensitive(pwLabel)
}