Google Analytics not working in Android webviews [duplicate] - javascript

I have a simple native Android app that is a webview of a website, effectively to make the mobile-ready site native-like if you will. The website already has Google Analytics installed.
What might be a good way to track which visitors are using the app?
I could adding Android Native App Tracking, but I presume that would
double track the users. Unless it's smart enough to connect the visits?
I could pass custom get variable to the site that maybe adds a custom
attribute to the tracking for native app users. But that doesn't
sound very clean.
What might be best for tracking? I feel there's got to be an obvious answer I'm missing.

that should help you:
Now getting back to the Analytics tracking of this web app, I used the code provided by Google here.
So the code becomes somewhat like this.
public class myWebApp extends Activity{
Webview mWebview;
GoogleAnalyticsTracker tracker;
protected void onCreate(Bundle savedInstanceState) {
tracker = GoogleAnalyticsTracker.getInstance();
// Start the tracker in manual dispatch mode. The following UA-xxxxxxx-x code must be replaced by //your web property ID.
tracker.startNewSession("UA-xxxxxxx-x", this);
mWebview = new WebView(this);
mWebview .setWebViewClient(new myWebViewClient());
mWebview .loadUrl("file:///android_asset/www/index.html");
private class myWebViewClient extends WebViewClient
{
//After the user visits a particular page, send the tracking notification to GoogleAnalytics.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
tracker.trackPageView( mWebview.getUrl());
tracker.dispatch();
}
}
}
http://www.the4thdimension.net/2011/11/using-google-analytics-with-html5-or.html
And in stats of google analytics you should get some info at least about operating system android.

Related

Android WebView push notification?

I need to send a notification (not necessarily a push notification) through an android webview. I saw that the Notification API was not compatible with Android Webview on MDN. The other APIs I saw seemed to be based off of window.notification.
Does anyone know of any API or JS that sends a notification through an android webview?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase. Would that be helpful?
Thanks for your answers!
I don't have enough "reputation" to post a comment but could that be useful to you?
Android Push Notification with WebView?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example
Read the documentation entry for Binding JavaScript code to Android code.
This allows you to use javascript to trigger the execution of android code.
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.
JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called. In this example show a toast.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑
Then you can trigger the android code from javascript like this:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself. But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.
You would need to provide the notifications for example as a .json file in JSON format. You can upload that .json file to your webserver somewhere. Then pass the content to android and parse it accordingly.
One Simple workaround use JSInterafce - communicate webview to native.tutorial
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.

what about JavaScript in android?

I am a android developer. I want to develop an android app but want to code in javascript for that. Is it possible using ReactNative?
http://www.reactnative.com/
You can defiantly write phone apps in JS, there are quite few options on the market:
On one hand there are hybrid apps, which written in HTML, CSS, JS and are built usually by something like cordova in order to communicate with phone API's. Some good frameworks that help you with the build and styling proccess are: Phonegap and Ionic.
And you have the react-native approach which basicly compiles the JS code to native phone components.
Both approaches let you reuse parts of your code in multiple platforms(Android, IOS).
While in hybrid apps you can reuse almost all of your code but just build for each platform. On react native you will have to code your views for each platform while your BL will stay the same if you written the code properly.
You have to use WebView for this. Then register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.
public class JavascriptInterfaceActivity extends Activity {
/** Called when the activity is first created. */
WebView wv;
JavaScriptInterface JSInterface;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
// register class containing methods to be exposed to JavaScript
JSInterface = new JavaScriptInterface(this);
wv.addJavascriptInterface(JSInterface, "JSInterface");
wv.loadUrl("file:///android_asset/myPage.html");
}
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
public void changeActivity()
{
Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);
startActivity(i);
finish();
}
}
}
Here is the html page
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
JSInterface.changeActivity();
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
Yes obviously you can use ReactNative to develop the android applications. You need to write the app code in javascript in this case.
You must use react native in this case. It has native components exposed to javascript. If you need some special component(That is not available in GitHub), you can write that native module in Android and IOS respectively. Also, it has a great community support.

How to make android app for existing dynamic website

I want to develop an android app like olx, and quikr for one of my website, Fusion120. Front end of website is in html, css, javascript, jquery and back end uses php and sql server. I don't know where and how to start, please Gide me.
you need to can webview in android app when icon is click
public class WebViewActivity extends Activity {
private WebView webView;
WebView mwebview;
ProgressDialog barProgressDialog;
ConnectionDetector cd;
Boolean status;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
String myurl = "http://www.fusion120.com";
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(myurl);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
PhoneGap SDK is very useful to make app using HTML, jquery-mobile and web-services. You can go for it and have a look at the tutorials. Very basic knowledge of android is enough to make your site in Android.
database which you use in the website can be exposed to other mobile devices and other applications via web services or REST services.
service development is depend on which technology, program language you going to use etc...
try this link
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
i hope your work is done by this answer

How to navigate to phone setting screen from a webapp

I am developing a web app using sencha touch for all mobile devices. My application uses GPS feature. I would like to know how can I navigate to the native setting screen of the phone from my web app for the user to switch ON the GPS feature.
If the GPS is not ON, it will prompt user a popup which will take the user to location settings screen to turn ON the GPS.
Is this possible ?
Please let know. I have seen this feature in many apps, even in google maps.
It is not cording practice to do that. You can simply state the steps to be followed.
IF you are running webapp on Phonegap or Android web view, then you can do like this.
Read this article how to bind Javascript function to Native app.
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
Android Code
public class WebAppInterface {
/** Show a toast from the web page */
#JavascriptInterface
public void goToSettingPage() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
JavaScript Code
<input type="button" value="Say hello" onClick="openSettingPage()" />
<script type="text/javascript">
function openSettingPage() {
Android.goToSettingPage();
}
</script>

Run custom javascript code after loading any website

I am working on taking readings about web browser performance and so need to access the window.performance object of the browser.
To collect this data i have written a javascript file, collect.js which i need to add to the DOM of the page that i need to test eg. www.google.com, www.facebook.com and so on...
Also i need to run this test for about 1000 websites, any manual approach is out of the question. I need it to be automated somehow.
How could i go about doing this?
EDIT: I need to run these tests on an android browser, so i need mobile oriented solutions.
You can create a simple android app with a WebView component. This way you can control which URLs are loaded and also insert your JS code.
http://developer.android.com/guide/tutorials/views/hello-webview.html
EDIT
You can run any javascript like this:
Implement a custom WebView:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// Execute your javascript below
view.loadUrl("javascript:...");
}
}
If you are looking for an automated solution, try PhantomJs this provides an automated headless web browser. Also has access to network traffic
perhaps you can try "bookmarklet"
http://www.bookmarklets.com/
the advantage over greasemonkey script is that it can run on
firefox and explorer

Categories

Resources