Dual Instances of SDK

From Engineering Client Portal

Revision as of 21:07, 1 December 2020 by ColinBrown (talk | contribs)

Engineering Portal / Digital / DCR & DTVR / Dual Instances of SDK

Multiple SDK Instances

SDK Initialization The latest version of the Nielsen App SDK allows instantiating multiple instances of the SDK object, which can be used simultaneously without any issue. The sharedInstance API that creates a singleton object was deprecated prior to version 5.1.1. (Version 4.0 for Android)

A maximum of four SDK instances per appid are supported. When four SDK instances exist, you must destroy an old instance before creating a new one.

Example using Swift V4

Download Project Files

In this example we create a swift file called NielsenInit where I store our SDK Initialization code. The example uses 2 APPIDS to keep everything organized.

 self.nielsenStaticApi = NielsenInit.createStaticNielsenApi(delegate: self)
 self.nielsenApi = NielsenInit.createNielsenApi(delegate: self)


Here are the methods in the NielsenInit class.

class NielsenInit : NSObject {

    class func createNielsenApi(delegate: NielsenAppApiDelegate) -> NielsenAppApi?{
        let appInformation:[String: String] = [          

            "appid": "PDA7D5EE6-B1B8-4123-9277-2A788BC65XXX",
            "appname": "Standard SDK Swift Sample",
            "appversion": "1.0",
            "sfcode": "dcr",
            "nol_devDebug": "INFO"
        ]     
        return NielsenAppApi(appInfo:appInformation, delegate:delegate)
    }  

    class func createStaticNielsenApi(delegate: NielsenAppApiDelegate) -> NielsenAppApi?{
 
        let appInformation:[String: String] = [

            "appid": "PE4A57F64-878D-4D69-8EFB-4E87C27BA7A4",
            "appname": "Standard SDK Swift Sample",
            "appversion": "1.0",
            "sfcode": "dcr",
            "nol_devDebug": "INFO"
        ]
        return NielsenAppApi(appInfo:appInformation, delegate:delegate)
    }
}


We then load up the Static and Content metadata in our sample:

    func loadStatic() -> [String : Any] {
        //Loading Static data
        let staticData =
        [
            "type": "static",
            "section": "siteSection",
            "segA": "segmentA",
            "segB": "segmentB",
            "segC": "segmentC"
        ]
        return staticData
    }

    func loadContent() -> [String : Any] {
        //Loading Content data
        url = NSURL(string: "http://www.nielseninternet.com/NielsenConsumer/prog_index.m3u8")
        let content = [
            "type":"content",
            "assetid":"C77664",
            "length":"3600",
            "program":"MyProgram",
            "segB":"CustomSegmentValueB",
            "segC":"segmentC",
            "title":"S2,E3",
            "section":"cloudApi_app",
            "airdate":"20180221 10:00:00",
            "isfullepisode":"y",
            "adloadtype":"2",
            "channelName":"My Channel 1",
            "pipMode":"false" ] as [String : Any]

        return content
    }


In AppDelegate, we initialize the 1st instance of the SDK for Static Measurement when the App launches.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //Getting the instance of NielsenApi
        self.nielsenStaticApi = NielsenInit.createStaticNielsenApi(delegate: self)
        sdkMethods = SDKMethods()
        self.data = sdkMethods.loadStatic()
        self.nielsenStaticApi.loadMetadata(self.data)
        return true
    }


Now for the Player Instance, we initialize in the ViewController, but only after the View has loaded:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "new_ios_bg.png")!)
        //In NielsenInit class we are initialising the NielsenApi.

        //Getting the instance of NielsenApi
        self.nielsencontentApi = NielsenInit.createNielsenApi(delegate: self)


Now you can just call them separately:

For example, App goes into the background:

    func applicationDidEnterBackground(_ application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        self.nielsenStaticApi.loadMetadata(self.data)
    }


Or sending the playhead position to the second instance:

nielsencontentApi.play(sdkMethods.loadChannelInfo())          
self.nielsencontentApi?.playheadPosition(pos);

Example using Android Studio

Download Project Files

The below is an example of Initializing two instances of the Nielsen SDK. One for Static Measurement, and the other for Video Measurement.

public class NielsenInit {

    private AppSdk mAppSdk = null;
    private AppSdk mStaticAppSdk = null;

    public AppSdk initAppSdk(Context mContext, IAppNotifier appNotifier){
        try {
            //Initialising the NielsenAppSDK class by passing app information which returns the instance of AppSdk.

            JSONObject appInformation = new JSONObject()

                    .put("appid", "PDA7D5EE6-B1B8-4123-9277-2A788BC653CA")
                    .put("appversion", "1.0")
                    .put("sfcode", "dcr")
                    .put("nol_devDebug", "INFO");

            mAppSdk = new AppSdk(mContext, appInformation, appNotifier);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mAppSdk;
    }

    public AppSdk initStaticAppSdk(Context mContext, IAppNotifier appNotifier){
        try {
            //Initialising the NielsenAppSDK class by passing app information which returns the instance of AppSdk.

            JSONObject appInformation = new JSONObject()

                    .put("appid", "PE4A57F64-878D-4D69-8EFB-4E87C27BA7A4")
                    .put("appversion", "1.0")
                    .put("sfcode", "dcr")
                    .put("nol_devDebug", "INFO");

            mStaticAppSdk = new AppSdk(mContext, appInformation, appNotifier);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mStaticAppSdk;
    }
}



        NielsenInit nielsenInit = new NielsenInit();

        //3rd parameter "this" referes to IAppNotifier interface which is needed to initialise NielsenSDK.
        staticAppSdk = nielsenInit.initStaticAppSdk(MyApplication.getContext(), this);
        data = sdkMethods.loadStaticData();

  if(this.b == 0) {
                        AppLaunchMeasurementManager.appInBackground(app.getApplicationContext());
                        staticAppSdk.loadMetadata(data);
                        Log.d("SdkBgFgDetectionUtility", "App going to background");
                    }



        //Getting the instance of NielsenSDK.
        NielsenInit nielsenInit = new NielsenInit();

        //3rd parameter "this" referes to IAppNotifier interface which is needed to initialise NielsenSDK.
        appSdk = nielsenInit.initAppSdk(getApplicationContext(), this);