1

I have a native android app that goes into a WebView for the checkout process. I am implementing appsflyer to track revenue through the app. How can I detect which button was clicked on the page, and what the item price is for revenue?

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AppsFlyerLib;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EventActivity extends AppCompatActivity {

    ProgressDialog pDialog;
    Boolean redirecting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        String appsFlyerUID = AppCore.getInstance(getApplicationContext()).getAppsFlyerUID();
        String idfa = AppCore.getInstance(getApplicationContext()).getMixpanelAdvertisingIdentifier();

        Intent intent = getIntent();

        try {
            JSONObject event = new JSONObject(intent.getStringExtra("event"));

            String eventId = event.getString("id");
            String title = event.getString("title");
            String startsAt = event.getString("starts_at");
            String venue = event.getString("venue");
            String city = event.getString("city");
            String state = event.getString("state");

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
            Date newDate = format.parse(startsAt);

            format = new SimpleDateFormat("EEEE, MMMM d @ h:mm a");
            String date = format.format(newDate);

            ((TextView)findViewById(R.id.event_title)).setText(title);
            ((TextView)findViewById(R.id.event_date)).setText(date);
            ((TextView)findViewById(R.id.event_venue)).setText(venue + " - " + city + ", " + state);

            String url = "http://tlapi.ticketliquidator.com/event_map/" + eventId + "?utm_campaign=" + idfa + "&utm_term=" + appsFlyerUID;


            Map<String, Object> eventValue = new HashMap<String, Object>();
            AppsFlyerLib.getInstance().trackEvent(getApplicationContext(), "View Event " + eventId,eventValue);


            final WebView wv = (WebView) findViewById(R.id.event_webview);
            wv.getSettings().setJavaScriptEnabled(true);
            final Activity self = this;

            wv.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    redirecting = true;
                    view.loadUrl(url);
                    return true;
                }
                public void onPageFinished(WebView view, String url) {
                    if(redirecting){
                        pDialog.dismiss();
                    }
                }
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    if(redirecting){
                        pDialog = new ProgressDialog(self);
                        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        pDialog.setMessage(" Loading...");
                        pDialog.setCancelable(true);
                        pDialog.show();
                    }

                }
            });


            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Getting event...");
            pDialog.show();

            wv.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {

                    if(progress == 100)
                        pDialog.dismiss();
                }
            });

            wv.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

This is my android class for the WebView. I am trying to pull information from this web page...

Api WebView

I just need to know which button was clicked in the WebView, and get the price beside that button. Then I will send that back to appsflyer for tracking.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
Trent Pierce
  • 387
  • 3
  • 22

2 Answers2

1

You'll have to trick it with java script as for that after loading the page you'll have to inject your javascript into the loaded webpage like this

@Override
public void onPageFinished(WebView view, String url){
   // your javascript as string which works fine in scratchpad
   String javaScript ="javascript:(function() {alert();})()"; 
   webview.loadUrl(javaScript);
}

So that that you can inject the javascript and find the respective DOM as per your requirement and based on that you can call java method like this.

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
0

you can't access the event of button click inside your webview, webview is used to run an html page, and for that you have to code inside your html page, use JavaScript to capture onClick event