The plugin only works for com.android.tools.build:gradle:1.x
Annotation-triggered tracking along with Google Analytics for Android.
@SendEvent(category = "HelloWorld", action = "sayHello", label="%1$s")
String sayHello (String name) {
return format("Hello, %s.", name);
}
Before using GAlette, make sure you have done following instructions described in https://developers.google.com/analytics/devguides/collection/android/v4/
- Before you begin
- Updating AndroidManifest.xml
- Initialize Trackers
- Create a configuration XML file
Add the GAlette plugin to your buildscript
's dependencies
section and apply the plugin:
buildscript {
repositories {
...
mavenCentral()
}
dependencies {
...
classpath 'com.uphyca.galette:galette-plugin:0.9.16'
}
}
...
apply plugin: 'com.uphyca.galette'
Implements TrackerProvider to your Application class
public class MyApplication extends Application implements TrackerProvider {
private Tracker mTracker;
@Override
public void onCreate() {
super.onCreate();
GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
mTracker = ga.newTracker(R.xml.your_tracker_resource);
}
@Override
public Tracker getByName(String trackerName) {
return mTracker;
}
}
Declare your application in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uphyca.example.galette">
...
<application
android:name=".MyApplication" ...>
...
</application>
</manifest>
Annotate @SendScreenView to your methods to track appView
public class MainActivity extends Activity {
@Override
@SendScreenView(screenName = "main")
protected void onCreate(Bundle savedInstanceState) {
...
}
}
Annotate @SendEvent to your methods to track event
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
@SendEvent(category = "button", action = "click")
public void onClick(View v) {
onButtonClicked();
}
});
}
}
Use string template to apply method parameters
public class MainActivity extends Activity {
private int mClickCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClicked(++mClickCount);
}
});
}
@SendEvent(category = "button", action = "click", label = "times", value = "%1$d")
private void onButtonClicked(int count) {
...
}
}
Use FieldBuilder to build each field value
public class MainActivity extends Activity {
@Override
@SendScreenView(screenName = "foo", screenNameBuilder = BundleValueFieldBuilder.class)
protected void onCreate(Bundle savedInstanceState) {
...
}
public static class BundleValueFieldBuilder implements FieldBuilder<String> {
@Override
public String build(Fields fields, String fieldValue, Object declaredObject, Method method, Object[] arguments) {
// annotation's screenName value paths to fieldValue, in this case 'foo'
return ((Bundle) arguments[0]).getString(fieldValue);
}
}
}
Use HitInterceptor to send arbitrary values
public class MyApplication extends Application implements TrackerProvider, HitInterceptor.Provider {
private Tracker mTracker;
private HitInterceptor hitInterceptor = new HitInterceptor() {
@Override
public void onEvent(EventFacade eventFacade) {
eventFacade.setCustomDimension(1, Build.MODEL);
}
@Override
public void onScreenView(ScreenViewFacade screenViewFacade) {
screenViewFacade.setCustomDimension(1, Build.MODEL);
}
};
@Override
public void onCreate() {
super.onCreate();
GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
ga.setLocalDispatchPeriod(1);
mTracker = ga.newTracker("SET-YOUR-TRACKING-ID");
}
@Override
public Tracker getByName(String trackerName) {
return mTracker;
}
@Override
public HitInterceptor getHitInterceptor(String trackerName) {
return hitInterceptor;
}
}
Following types or these subclasses are supported.
- Application
- Activity
- Service
- Fragment (android.app or android.support.v4.app)
- View
- Context
or implements ContextProvider to orbitary classes.
public class MyClass implements ContextProvider {
private Context mContext;
public MyClass(Context context) {
mContext = context;
}
@Override
public Context get() {
return mContext;
}
@SendScreenView(screenName = "my-class")
void foo() {
}
}
configurations.all {
resolutionStrategy {
force "com.google.android.gms:play-services-analytics:${playservicesVersion}"
}
}
See more details ResolutionStrategy - Gradle DSL Version 2.8
Proguard configuration is automatically applied.
Copyright 2014-2021 uPhyca Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.