Android SDK (Previous) - Handling Cases Where Tracking Links Open in WebView

    Overview


    When an Airbridge tracking link opens within your app's WebView environment, the deep linking user experience can be degraded compared to when it opens in another app. This document describes how to resolve this issue by having the Airbridge SDK open the tracking link instead of the WebView when a tracking link is clicked within the WebView environment.

    This document will guide you through the following steps to resolve the issue:

    1. An explanation of the Airbridge SDK function that opens tracking links.

    2. Detecting when a tracking link is clicked within the app's WebView.

    3. Configuring the Airbridge SDK to open the tracking link instead.

    Attention

    If another company's tracking link is opened in your app, and that other company's app is not installed and the tracking link's fallback is set to a web URL, the fallback web page will open in the user's default browser, not the WebView.

    The Airbridge SDK provides a function called Placement Click which allows you to open tracking links from the native part of your application.

    You can use this feature with the following code. You can also pass an AirbridgeCallback<Boolean> as a parameter to Airbridge.click to receive a callback for the response.

    12
    var trackingLink: String = "..."
    Airbridge.click(trackingLink)

    In WebViewClient, you can implement WebViewClient#shouldOverrideUrlLoading to detect when a specific URL is being loaded and decide whether to proceed with loading it or to stop it. Follow the steps below to implement the code to detect if the URL is an Airbridge tracking link.

    📘WebViewClient Android Developer Documentation

    Using WebViewClient

    To implement WebViewClient#shouldOverrideUrlLoading, create a webViewClient instance and set it to the webView using webView#setWebViewClient. The result of WebViewClient#shouldOverrideUrlLoading returns whether the WebResourceRequest was handled. Implement WebViewClient#shouldOverrideUrlLoading to detect which URL is being loaded. The URL being loaded is passed in the form of request.url from the WebResourceRequest function argument.

    12345678910111213
    private val webViewClient = object : WebViewClient() {
    
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            return super.shouldOverrideUrlLoading(view, request)
        }
    }
    
    private fun initWebview() {
        webView.webViewClient = webViewClient
    }

    Among the URLs being loaded, identify the ones that are Airbridge tracking links. An Airbridge tracking link will have a URL host of abr.ge, YOUR_APP_NAME.deeplink.page, or a custom domain.

    📘 You can find YOUR_APP_NAME in your dashboard under Settings > Tokens > App Name.

    1234567891011121314151617181920
    override fun shouldOverrideUrlLoading(
        view: WebView?,
        request: WebResourceRequest?
    ): Boolean {
    
        request?.url?.let {
    
            val airbridgeHosts = arrayOf(
                "abr.ge",
                "YOUR_APP_NAME.abr.ge",
                "YOUR_APP_NAME.deeplink.page"
            )
    
            if (airbridgeHosts.contains(it.host)) {
                return true
            }
        }
    
        return false
    }


    With the steps above, you can now detect when an Airbridge tracking link is loaded in a WebView. When a link is detected, use the Airbridge.click function and return true from webView#shouldOverrideUrlLoading. This will have the Airbridge SDK open the tracking link instead of the WebView.

    12345678910111213141516171819202122232425
    override fun shouldOverrideUrlLoading(
        view: WebView?,
        request: WebResourceRequest?
    ): Boolean {
    
        request?.url?.let {
    
            val airbridgeHosts = arrayOf(
                "abr.ge",
                "YOUR_APP_NAME.abr.ge",
                "YOUR_APP_NAME.deeplink.page"
            )
    
            if (airbridgeHosts.contains(it.host)) {
                decisionHandler(it)
                return true
            }
        }
    
        return false
    }
    
    private fun decisionHandler(uri : Uri) {
        Airbridge.click(uri.toString())
    }

    Conclusion


    The implementation is now complete. Below is the full code used in this guide.

    12345678910111213141516171819202122232425262728293031323334353637383940
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout....)
        initWebView()
    }
    
    private fun initWebView() {
        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.webViewClient = webViewClient
    }
    
    private val webViewClient = object : WebViewClient() {
    
        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
    
            request?.url?.let {
                val airbridgeHosts = arrayOf(
                    "abr.ge",
                    "YOUR_APP_NAME.abr.ge",
                    "YOUR_APP_NAME.deeplink.page"
                )
    
                if (airbridgeHosts.contains(it.host)) {
                    decisionHandler(it)
                    return true
                }
            }
    
            return false
        }
    
        private fun decisionHandler(uri: Uri) {
            Airbridge.click(uri.toString())
        }
    }

    Was this helpful?

    Any questions or suggestions?