Piwik Android SDK with Emulator


(Nam Nguyen Hoai) #1

Hi,

I’ve tested Piwik with website tracking and everything works just fine to make sure that the problem won’t come from the server side. However, when I tried with a simple Android application, and I followed all the instructions step by step from here piwik-sdk-android/README.md at e59c146c9beefd9a731b749b7e3532d199cf5e84 · matomo-org/piwik-sdk-android · GitHub

I run my application on a simulator in Android Studio, and I couldn’t any data in my Piwik dashboard. Will the simulator in Android studio send data to the Piwik server? Because I didn’t see any request coming to my Piwik web server.
Anyone has the experience testing with Piwik Android SDK can give me some guidance. Thank you in advance!

Nam


#2

Why are you using the README from 2014?
Follow the README from the master branch, i.e. latest release.

AFAIK there are no issues with the current SDK release.

Check whether you get an 200/204 response from the server for your transmission.

If you are not getting the help you need, try providing more information so it is easier for others to help you, possible a sample project that doesn’t work.


(Nam Nguyen Hoai) #3

Hi @darken, thanks for your help. Here is the file in my Android project

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trungel.helloworld">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My app bundle (I built the .jar file from source code so I didn’t put the compile of piwik here)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trungel.helloworld">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My MainApplication

package com.example.trungel.helloworld;

import android.app.Application;
import android.util.Log;

import java.net.MalformedURLException;

import org.piwik.sdk.Piwik;
import org.piwik.sdk.Tracker;
import org.piwik.sdk.TrackerConfig;
import org.piwik.sdk.extra.PiwikApplication;

import timber.log.Timber;

public class MainApplication extends Application {
    private Tracker piwikTracker;

    synchronized public Tracker getTracker(){
        if (piwikTracker == null) {
            piwikTracker = Piwik.getInstance(this).newTracker(new TrackerConfig("https://localhost/analytics/piwik.php", 6, "helloworld"));
            //piwikTracker = Piwik.getInstance(this).newTracker("https://localhost/analytics/piwik.php", 6); % this gave me an error
        }

        return piwikTracker;
    }
}

my MainActivity

package com.example.trungel.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import org.piwik.sdk.Tracker;
import org.piwik.sdk.extra.PiwikApplication;
import org.piwik.sdk.extra.TrackHelper;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Tracker tracker = ((MainApplication) getApplication()).getTracker();

    }

    private Tracker getTracker(){
        return ((PiwikApplication) getApplication()).getTracker();
    }

    public void piwikCount(View view) {
        Tracker tracker = ((MainApplication) getApplication()).getTracker();
        TrackHelper.track().event("Clicks", "Button").with(tracker);
    }
}

I was trying to track the number of clicks of a really simple button which is given in the function piwikCount. However when I ran the application with the emulator, I didn’t receive any data to the server as well as there is no data in the dashboard.

Has anyone had experience testing Piwik Android SDK with the Emulator? Thanks

Nam


#4

I don’t the Android device itself is running a Piwik (err Matomo) server, so this won’t work:

 piwikTracker = Piwik.getInstance(this).newTracker(new TrackerConfig("https://localhost/analytics/piwik.php", 6, "helloworld"));

localhost is the device itself, i.e. 127.0.0.1, i.e. the device itself.


(Nam Nguyen Hoai) #5

Hi @darken, thanks for the answer. I’ve also figured it out myself yesterday that I shouldn’t have put localhost in for the URL. Instead I put 10.0.2.2 which is the address IP of my own computer regarding from the emulator. It worked.