The JavaScript SDK works best with device identifier information, which is only accessible via native code. Here are some examples of how to pass the native identifiers into your TUNE instance in JavaScript.
First, create a custom class that contains the methods you want to call from JavaScript. The following code example shows a class that gets the Google AID and isLimitAdTrackingEnabled setting:
For iOS, you can use the stringByEvaluatingJavaScriptFromString method of UIWebView to pass native code by calling a JavaScript function: UIWebView Class Reference
The following native code example shows retrieval of the IFA/IFV identifiers and passing them to a “setIdentifiers” function:
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
public class CustomNativeAccess {
private final Context mContext;
private String gaid;
private boolean isLAT;
public CustomNativeAccess(Context context) {
mContext = context;
new Thread(new Runnable() {
@Override
public void run() {
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
gaid = adInfo.getId();
isLAT = adInfo.isLimitAdTrackingEnabled();
} catch (Exception e) {
}
}
}).start();
}
@JavascriptInterface
public String getGaid() {
return gaid;
}
@JavascriptInterface
public boolean getIsLAT() {
return isLAT;
}
}
ASIdentifierManager *adMgr = [ASIdentifierManager sharedManager];
NSString *appleAdId = [[adMgr advertisingIdentifier] UUIDString];
NSString *adTrackingEnabled = [NSString stringWithFormat:@"%d", adMgr.advertisingTrackingEnabled];
NSString *appleVendorId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSString *javascript = [NSString stringWithFormat:@"setIdentifiers('%@', '%@', '%@')",
appleAdId, adTrackingEnabled, appleVendorId];
// Create a UIWebView or grab your existing one
[yourWebView stringByEvaluatingJavaScriptFromString:javascript];
Unable to load code example.
Select a preferred platform.
In your main Activity, instantiate the class and add it to the WebView with addJavascriptInterface before displaying your html, passing it the name of the interface that you use to access it in the JavaScript:
Your JavaScript code should also have the same function that simply sets these identifiers for the MAT instance:
public class NativeToJSExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a WebView or grab your existing one before this step
yourWebView.addJavascriptInterface(new CustomNativeAccess(this), "android");
}
}
function setIdentifiers(ifa, trackingEnabled, ifv) {
MobileAppTracker.setAppleAdvertisingIdentifier(ifa, trackingEnabled);
MobileAppTracker.setAppleVendorIdentifier(ifv);
}
Unable to load code example.
Select a preferred platform.
Now that you’ve passed the interface as “android”, you can access it in your JavaScript:
var gaid = window.android.getGaid();
var isLAT = window.android.getIsLAT();
MobileAppTracker.setGoogleAdvertisingId(gaid, isLAT);
Unable to load code example.
Select a preferred platform.
No Comments