Search code examples
androidwebviewmatomo

WebView will not run PIWIK tracking JS


I created an android app from code that I use with another app of mine. When people use the app, the PIWIK tracking JS will not run. If you visit the website, it tracks fine (The JS tracker is a JS tag on the webpage). If you use the app, it will not track the visit. I have JS enabled, and I copied the code from a app where it was working fine. The only difference in between the two apps is the SDK target versions.

Here is my code below:

public class ZestyView extends Activity {

final Activity activity = this;
WebView myWebView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zestyview);
    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);
    Toast.makeText(getApplicationContext(), "Page is loading... Please wait!", Toast.LENGTH_LONG).show();
    myWebView.loadUrl("https://zestysaver.com/?pk_campaign=App&pk_kwd=Android");
}


private class MyWebViewClient extends WebViewClient {


    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("zestysaver.com")) {
            Log.e("url", Uri.parse(url).getHost());
            return false;
        }
        // Otherwise, give the default behavior (open in browser)
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

The tracking code is hosted on an external domain.


Solution

  • Finally found my answer. I got to looking through my LogCat and found a line that was not marked as an error:

    I/X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
    

    Did a little bit of researching to make sure I was right, then ran my website that hosts my analytical software (PIWIK) through SSLLabs, and found the cert chain was not complete. Updated the cert chain on my website, and bam.. started tracking.

    Man, I have spent hours on something so silly!