I have a keyboard app for Android that I'm developing, and it outputs simple symbols rather than language, so that said, I would love to be able to track user activity since there's no sensitive information or words involved.
The problem is that Android's InputMethodService
does not extend Application
, which is what allows you to access Google Analytics' Android SDK (possible wording error, here, feel free to correct me).
I've followed the guide here to get started, and this is the code I referenced to acquire the Tracker
object:
/*
* Copyright Google Inc. All Rights Reserved.
*
* 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.
*/
package com.google.samples.quickstart.analytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
/**
* This is a subclass of {@link Application} used to provide shared objects for this app, such as
* the {@link Tracker}.
*/
public class AnalyticsApplication extends Application {
private Tracker mTracker;
/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
This is all great for tracking my app's main activity, which is basically just a view containing short set of instructions with a couple of ads and a settings shortcut.
As I said before, I'd like to track the keyboard, and how to do that isn't exactly obvious since InputMethodService
doesn't expose Google Analytics.
How can I utilize the Google Analytics Android SDK inside of a class that extends InputMethodService
but not Application
?
Please tell me if I haven't made my issue clear, I will update the post any way I can.
It's not necessary that you have an
Application
to use the Google Analytics' Android SDK.The example adds the helper method
getDefaultTracker
inside theApplication
class to centralize and ease the access to the default tracker. In most cases this would be the best possible solution, for this reason the example recommends this approach. But there are some exceptions where this solution is not feasible, like in theInputMethodService
.As you can see in the documentation the parameter of the method
getInstance
is aContext
:For this reason you can use the very same
getDefaultTracker
method directly inside yourInputMethodService
. For example:then you can use the methods
getDefaultTracker
in every method of your service.