Dual Instances of SDK
From Engineering Client Portal
Multiple SDK Instances
SDK Initialization
App SDK
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.
Browser SDK
The Browser SDK allows instantiating multiple instances of the object, which can be used simultaneously without any problems, however they must be instantiated with their own unique instance names. There is no maximum number of instances that is enforced in the browser.
App SDK Examples
Example using Swift
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-4E87C27BXXXX",
"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-2A788BC65XXX")
.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-4E87C27BAXXX")
.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);
Browser SDK Examples
The below examples will make use of the same AppId, however using different AppIds will work as well. The principles below follow the same initialization rules as a single instance and simply require a unique instance name and variable name with which the returned instance is assigned.
For more information around the possible parameters that can be passed as well as deeper descriptions for the API calls, please refer to our API documentation here: Browser SDK API Reference
Example of 2 instances on the same page
//Add Static Queue Snippet
<script>
!function (e, n) {
function t(e) {
return "object" == typeof e ? JSON.parse(JSON.stringify(e)) : e
}
e[n] = e[n] ||
{
nlsQ: function (o, r, c) {
var s = e.document,
a = s.createElement("script");
a.async = 1,
a.src = ("http:" === e.location.protocol ? "http:" : "https:") + "//cdn-gl.imrworldwide.com/conf/" + o + ".js#name=" + r + "&ns=" + n;
var i = s.getElementsByTagName("script")[0];
return i.parentNode.insertBefore(a, i),
e[n][r] = e[n][r] || {
g: c || {},
ggPM: function (o, c, s, a, i) { e[n][r].q = e[n][r].q || []; try { var l = t([o, c, s, a, i]); e[n][r].q.push(l) } catch (e) { console && console.log && console.log("Error: Cannot register event in Nielsen SDK queue.") } },
trackEvent: function (o) { e[n][r].te = e[n][r].te || []; try { var c = t(o); e[n][r].te.push(c) } catch (e) { console && console.log && console.log("Error: Cannot register event in Nielsen SDK queue.") } }
},
e[n][r]
}
}
}(window, "NOLBUNDLE");
//Create SDK Instance(s)
// Notice the unique instance name given to each instance below and the unique variable name for each instance
var nSdkInstanceLive = NOLBUNDLE.nlsQ("XXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX", "instanceLive", {
nol_sdkDebug: 'debug',
optout: 'false'
});
var nSdkInstanceVod = NOLBUNDLE.nlsQ("XXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX", "instanceVod", {
nol_sdkDebug: 'debug',
optout: 'false'
});
</script>
Example of 2 instances using a parent page and iFrame
Parent Page (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>DCR Static/DCR Video</title>
<script>
/* NLSQUEUE build v*/
!function(e,n){
function t(e){
return"object"==typeof e?JSON.parse(JSON.stringify(e)):e
}
e[n]=e[n]||
{
nlsQ:function(o,r,c){
var s=e.document,
a=s.createElement("script");
a.async=1,
a.src=("http:"===e.location.protocol?"http:":"https:")+"//cdn-gl.imrworldwide.com/conf/"+o+".js#name="+r+"&ns="+n;
var i=s.getElementsByTagName("script")[0];
return i.parentNode.insertBefore(a,i),
e[n][r]=e[n][r]||{g:c||{},
ggPM:function(o,c,s,a,i){e[n][r].q=e[n][r].q||[];try{var l=t([o,c,s,a,i]);e[n][r].q.push(l)}catch(e){console&&console.log&&console.log("Error: Cannot register event in Nielsen SDK queue.")}},
trackEvent:function(o){e[n][r].te=e[n][r].te||[];try{var c=t(o);e[n][r].te.push(c)}catch(e){console&&console.log&&console.log("Error: Cannot register event in Nielsen SDK queue.")}}},
e[n][r]
}
}
}(window,"NOLBUNDLE");
// SDK Initialization
var nSdkInstance = NOLBUNDLE.nlsQ("XXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX", "staticPage", {
nol_sdkDebug: 'debug',
});
// Static Metadata
var staticMetadata = {
assetid: 'staticTestPage',
type: 'static'
};
// Event 'staticstart' call
nSdkInstance.ggPM(14, staticMetadata);
</script>
</head>
<body>
<h1>DCR Static on Parent/DCR Video in iFrame</h1>
<p>Static measurement is current on parent page when window is in focus</p>
<iframe src="./dcr-video.html" frameborder="0" width="650" height="500" style="background-color: rgb(42, 217, 248);" id="video-iframe"></iframe>
</body>
</html>
Nested iFrame Referenced on Parent Page (dcr-video.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>iFrame Same Source</title>
<script>
/* NLSQUEUE build v*/
!function(e,n){
function t(e){
return"object"==typeof e?JSON.parse(JSON.stringify(e)):e
}
e[n]=e[n]||
{
nlsQ:function(o,r,c){
var s=e.document,
a=s.createElement("script");
a.async=1,
a.src=("http:"===e.location.protocol?"http:":"https:")+"//cdn-gl.imrworldwide.com/conf/"+o+".js#name="+r+"&ns="+n;
var i=s.getElementsByTagName("script")[0];
return i.parentNode.insertBefore(a,i),
e[n][r]=e[n][r]||{g:c||{},
ggPM:function(o,c,s,a,i){e[n][r].q=e[n][r].q||[];try{var l=t([o,c,s,a,i]);e[n][r].q.push(l)}catch(e){console&&console.log&&console.log("Error: Cannot register event in Nielsen SDK queue.")}},
trackEvent:function(o){e[n][r].te=e[n][r].te||[];try{var c=t(o);e[n][r].te.push(c)}catch(e){console&&console.log&&console.log("Error: Cannot register event in Nielsen SDK queue.")}}},
e[n][r]
}
}
}(window,"NOLBUNDLE");
// SDK Initialization
var nSdkInstance = NOLBUNDLE.nlsQ("XXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX", "videoPlayer", {
nol_sdkDebug: 'debug',
});
var contentMetadata = {
type: "content",
length: "300",
title: "Skydiving Across Europe",
program: "latacg",
censuscategory: "Enlisted",
assetid: "204558915992",
section: "ProgramAsset8",
tv: "true",
adModel: "0",
dataSrc: "cms",
segA: "S07E04:NBC",
segB: "teams",
segC:"AtlanataFalcons",
}
</script>
</head>
<body>
<p>I'm iframe SDK DCR Video measurement, DCR static measurement should continue when clicking "play" on video</p>
<main>
<video
controls
src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"
width="620">
Sorry, your browser doesn't support embedded videos, but don't worry, you can
<a href="https://archive.org/details/BigBuckBunny_124">download it</a>
and watch it with your favorite video player!
</video>
</main>
<script src="../player.js"></script>
</body>
</html>