Register WebViews
Starting from NAM SDK 8.2.4, if you use a WebView in your app to display ads, you must register each WebView object with the SDK. This is a critical feature to improve ad targeting accuracy and enhance ad revenue by enabling communication between the NAM Web SDK loaded in the WebView and the website. Follow the guide below to complete the registration process for all cases where a WebView is used, including web apps.
This guide explains how to register a WebView object to improve ad targeting effectiveness.
Allow Third-Party Cookies
To improve the user's ad experience and comply with Chrome's cookie policy, you need to enable third-party cookies in the WebView instance.
- Kotlin
- Java
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
Configure WebView
The default WebView settings are not optimized for ads. Use the WebSettings API to configure the WebView as shown in the example below.
- Kotlin
- Java
import android.webkit.CookieManager
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled = true
// Let the web view access local storage.
webView.settings.domStorageEnabled = true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture = false
}
}
import android.webkit.CookieManager;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
}
Register WebView
Call GfpSdk.registerWebView() on the UI Thread to establish a connection between the NAM Web SDK JavaScript handler and each WebView object. This should be done as early as possible, for example, in the onCreate() method of the main activity. Ensure this is done before calling loadUrl().
- Kotlin
- Java
import android.webkit.CookieManager
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webView)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled = true
// Let the web view access local storage.
webView.settings.domStorageEnabled = true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture = false
// Register the webView on GfpSdk.
// Should be run on UI Thread.
GfpSdk.registerWebView(webView)
// Load url on the webView.
webView.loadUrl(url)
}
}
import android.webkit.CookieManager;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// Register the webView on GfpSdk.
// Should be run on UI Thread.
GfpSdk.registerWebView(webView);
// Load url on the webView.
webView.loadUrl(url);
}
}
Verify WebView Registration
After registering the WebView, you can verify whether it has been successfully registered using the GfpSdk.isRegisteredWebView() method. If successfully registered, the callback will return true; otherwise, it will return false.
- Kotlin
- Java
GfpSdk.isRegisteredWebView(webView) { result ->
if (result) {
// WebView is registered
} else {
// WebView is not registered
}
}
GfpSdk.isRegisteredWebView(webView, (result -> {
if (result) {
// WebView is registered
} else {
// WebView is not registered
}
}));