2

I track my visitors' usernames and I want to pass the username data from webpage to my activity method. What is the proper way to pass this data ?

(Putting username data inside HTML page's hidden div and parsing webpage is my last option, I don't prefer it)

I use Webview in my Android application. I load URLs like this:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
  ...
  String url = "http://example.com";
  webView.postUrl(url, EncodingUtils.getBytes(myPostData, "base64"));
  ...
}

There is some custom headers inside my page response. Response headers have this:

header('User name: michael');

If page is successfully loaded, I need to get that headers from the response and use it inside my Google Analytics as custom dimension.

 EasyTracker easyTracker = EasyTracker.getInstance();
 easyTracker.send(MapBuilder
    .createAppView("Home screen")
    .set(Fields.customDimension(1), "michael");
    .build()
);

My problem is what is the proper way for this ? Is it possible to get it from onPageFinished and set this header inside Analytics code or is there a better way ? I'm open to any method/solution that is compatible with this structure.

Note that I override these methods for my activity works:

WebViewClient::onPageStarted(WebView view, String url, Bitmap favicon) 
WebViewClient::onPageFinished(WebView view, String url) 
WebViewClient::shouldOverrideUrlLoading(WebView view, String url) 
WebViewClient::onLoadResource(WebView view, String url) 
WebViewClient::onReceivedError(WebView view, int errorCode, String description, String failingUrl)
WebChromeClient::onProgressChanged(WebView view, int newProgress) 

Edit:
There is a similar answer here which is 3 years old: https://stackoverflow.com/a/3134609/429938
But It offers to catch headers in shouldOverrideUrlLoading. Is it not possible to catch it in onPageFinished ? Or how can I use it with Webview.postUrl()

Edit2:
Another solution is to use addJavascriptInterface and set user name from the javascript code and set google analytics code inside activity. But this is a Android specific solution. I would prefer a common solution that can be used in IOS etc.

Community
  • 1
  • 1
trante
  • 33,518
  • 47
  • 192
  • 272
  • What you're asking is forbidden under the Google Analytics terms of service. http://www.google.com/analytics/terms/us.html "You will not (...) use the Service to track, collect or upload any data that personally identifies an individual (...), or other data which can be reasonably linked to such information by Google" – Eduardo Dec 04 '13 at 17:44
  • Username was just an example to express my problem. I set different usage data with analaytics. – trante Dec 04 '13 at 18:10

3 Answers3

2

the most cross platform solution is a javascript bridge ( addJavascriptInterface ). This is what I would recommend you.

The native part can be implemented on iOS too. And the HTML part stay the same.

If not, you need to implement all the logic on the native side using one of the events, like onPageFinished. If the header are not accessible from here, do the request yourself with HTTPURLConnection then loadDataWithBaseURL.

Loda
  • 1,970
  • 2
  • 20
  • 40
1

I'm using following code to konw when page is loaded:

 webview.setWebChromeClient(new WebChromeClient() {
                     public void onProgressChanged(WebView view, int progress)   
                     {
                      MyActivity.setTitle("Loading...");
                      MyActivity.setProgress(progress * 100); 

                        if (progress == 100) {
    //page is successfully loaded and you can get our header.

                         }
                       }
                     });
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
1

Try this one: here I'm getting "User name" value from header

HttpURLConnection conn = null;
            final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
           };
            try {
                URL url = new URL(YOUR_URL);
                if (url.getProtocol().toLowerCase().equals("https")) {
                    trustAllHosts();
                    HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
                    https.setHostnameVerifier(DO_NOT_VERIFY);
                    conn = https;
                } else {
                    conn = (HttpURLConnection) url.openConnection();
                }
                conn.setInstanceFollowRedirects(false);
                conn.connect();
        String location = conn.getHeaderField( "User name" );
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138