|
11 | 11 |
|
12 | 12 | import java.util.Map;
|
13 | 13 |
|
14 |
| -import android.app.Application; |
15 |
| - |
| 14 | +import android.content.Context; |
| 15 | + |
| 16 | +/** |
| 17 | + * The main interface to AdjustIo. |
| 18 | + * |
| 19 | + * Use the methods of this class to tell AdjustIo about the usage of your app. |
| 20 | + * See the README for details. |
| 21 | + * |
| 22 | + * @author wellle |
| 23 | + * @since 11.10.12 |
| 24 | + */ |
16 | 25 | public class AdjustIo {
|
17 |
| - // Tell AdjustIo that the application did launch. This is required to |
18 |
| - // initialize AdjustIo. Call this in the onCreate method of your launch |
19 |
| - // activity. |
20 |
| - public static void appDidLaunch(Application app) { |
21 |
| - if (!Util.checkPermissions(app)) { |
| 26 | + |
| 27 | + /** |
| 28 | + * Tell AdjustIo that the application did launch. |
| 29 | + * |
| 30 | + * This is required to initialize AdjustIo. |
| 31 | + * Call this in the onCreate method of your launch activity. |
| 32 | + * |
| 33 | + * @param context Your application context |
| 34 | + * Generally obtained by calling getApplication() |
| 35 | + */ |
| 36 | + |
| 37 | + public static void appDidLaunch(Context context) { |
| 38 | + if (!Util.checkPermissions(context)) { |
22 | 39 | return;
|
23 | 40 | }
|
24 | 41 |
|
25 |
| - String macAddress = Util.getMacAddress(app); |
| 42 | + String macAddress = Util.getMacAddress(context); |
26 | 43 |
|
27 |
| - appId = app.getPackageName(); |
| 44 | + packageName = context.getPackageName(); |
28 | 45 | macSha1 = Util.sha1(macAddress);
|
29 | 46 | macShort = macAddress.replaceAll(":", "");
|
30 |
| - userAgent = Util.getUserAgent(app); |
31 |
| - androidId = Util.getAndroidId(app); |
32 |
| - attributionId = Util.getAttributionId(app); |
| 47 | + userAgent = Util.getUserAgent(context); |
| 48 | + androidId = Util.getAndroidId(context); |
| 49 | + attributionId = Util.getAttributionId(context); |
33 | 50 |
|
34 | 51 | trackSessionStart();
|
35 | 52 | }
|
36 | 53 |
|
37 |
| - // Track any kind of event. You can assign a callback url to the event which |
38 |
| - // will get called every time the event is reported. You can also provide |
39 |
| - // parameters that will be forwarded to these callbacks. |
40 |
| - public static void trackEvent(String eventId) { |
41 |
| - trackEvent(eventId, null); |
| 54 | + |
| 55 | + /** |
| 56 | + * Track any kind of event. |
| 57 | + * |
| 58 | + * You can assign a callback url to the event which |
| 59 | + * will get called every time the event is reported. You can also provide |
| 60 | + * parameters that will be forwarded to these callbacks. |
| 61 | + * |
| 62 | + * @param eventToken The token for this kind of event |
| 63 | + * It must be exactly six characters long |
| 64 | + * You create them in your dashboard at http://www.adjust.io |
| 65 | + * @param parameters An optional dictionary containing callback parameters |
| 66 | + * Provide key-value-pairs to be forwarded to your callbacks |
| 67 | + */ |
| 68 | + |
| 69 | + public static void trackEvent(String eventToken) { |
| 70 | + trackEvent(eventToken, null); |
42 | 71 | }
|
43 | 72 |
|
44 |
| - public static void trackEvent(String eventId, Map<String,String> parameters) { |
45 |
| - String paramString = Util.getBase64EncodedParameters(parameters); |
| 73 | + public static void trackEvent(String eventToken, Map<String, String> parameters) { |
| 74 | + if (eventToken.length() != 6) { |
| 75 | + Logger.error( |
| 76 | + "Event tracking only works with proper event tokens. " + |
| 77 | + "Find them in your dashboard at http://www.adjust.io " + |
| 78 | + |
| 79 | + ); |
| 80 | + return; |
| 81 | + } |
46 | 82 |
|
47 |
| - RequestTask requestTask = new RequestTask("/event"); |
48 |
| - requestTask.setSuccessMessage("Tracked event " + eventId + "."); |
49 |
| - requestTask.setFailureMessage("Failed to track event " + eventId + "."); |
50 |
| - requestTask.setUserAgent(userAgent); |
51 |
| - requestTask.execute("id", eventId, "app_id", appId, "mac", macShort, "android_id", androidId, "params", paramString); |
| 83 | + String paramString = Util.getBase64EncodedParameters(parameters); |
| 84 | + String successMessage = "Tracked event: '" + eventToken + "'"; |
| 85 | + String failureMessage = "Failed to track event: '" + eventToken + "'"; |
| 86 | + |
| 87 | + TrackingPackage event = new TrackingPackage.Builder() |
| 88 | + .setPath("/event") |
| 89 | + .setSuccessMessage(successMessage) |
| 90 | + .setFailureMessage(failureMessage) |
| 91 | + .setUserAgent(userAgent) |
| 92 | + .addTrackingParameter(EVENT_TOKEN, eventToken) |
| 93 | + .addTrackingParameter(PACKAGE_NAME, packageName) |
| 94 | + .addTrackingParameter(MAC_SHORT, macShort) |
| 95 | + .addTrackingParameter(ANDROID_ID, androidId) |
| 96 | + .addTrackingParameter(PARAMETERS, paramString) |
| 97 | + .build(); |
| 98 | + getRequestThread().track(event); |
52 | 99 | }
|
53 | 100 |
|
54 |
| - // Tell AdjustIo that the current user generated some revenue. The amount is |
55 |
| - // measured in cents and rounded to on digit after the decimal point. If you |
56 |
| - // want to differentiate between various types of revenues you can do so by |
57 |
| - // providing different eventIds. If your revenue events have callbacks, you |
58 |
| - // can also pass in parameters that will be forwarded to your server. |
| 101 | + |
| 102 | + /** |
| 103 | + * Tell AdjustIo that the current user generated some revenue. |
| 104 | + * |
| 105 | + * The amount is measured in cents and rounded to on digit after the decimal |
| 106 | + * point. If you want to differentiate between various types of specific revenues |
| 107 | + * you can do so by using different event tokens. If your revenue events have |
| 108 | + * callbacks, you can also pass in parameters that will be forwarded to your |
| 109 | + * server. |
| 110 | + * |
| 111 | + * @param amountInCents The amount in cents (example: 1.5f means one and a half cents) |
| 112 | + * @param eventToken The token for this revenue event (see above) |
| 113 | + * @param parameters Parameters for this revenue event (see above) |
| 114 | + */ |
| 115 | + |
59 | 116 | public static void trackRevenue(float amountInCents) {
|
60 | 117 | AdjustIo.trackRevenue(amountInCents, null);
|
61 | 118 | }
|
62 | 119 |
|
63 |
| - public static void trackRevenue(float amountInCents, String eventId) { |
64 |
| - AdjustIo.trackRevenue(amountInCents, eventId, null); |
| 120 | + public static void trackRevenue(float amountInCents, String eventToken) { |
| 121 | + AdjustIo.trackRevenue(amountInCents, eventToken, null); |
65 | 122 | }
|
66 | 123 |
|
67 |
| - public static void trackRevenue(float amountInCents, String eventId, Map<String,String> parameters) { |
| 124 | + public static void trackRevenue(float amountInCents, String eventToken, Map<String, String> parameters) { |
| 125 | + if (eventToken != null && eventToken.length() != 6) { |
| 126 | + Logger.error( |
| 127 | + "Specific revenue tracking only works with proper event tokens. " + |
| 128 | + "Find them in your dashboard at http://www.adjust.io " + |
| 129 | + |
| 130 | + ); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
68 | 134 | int amountInMillis = Math.round(10 * amountInCents);
|
| 135 | + amountInCents = amountInMillis/10.0f; // now rounded to one decimal point |
69 | 136 | String amount = Integer.toString(amountInMillis);
|
70 | 137 | String paramString = Util.getBase64EncodedParameters(parameters);
|
| 138 | + String successMessage = "Tracked revenue: " + amountInCents + " Cent"; |
| 139 | + String failureMessage = "Failed to track revenue: " + amountInCents + " Cent"; |
| 140 | + |
| 141 | + if (eventToken != null) { |
| 142 | + String eventString = " (event token: '" + eventToken + "')"; |
| 143 | + successMessage += eventString; |
| 144 | + failureMessage += eventString; |
| 145 | + } |
| 146 | + |
| 147 | + TrackingPackage revenue = new TrackingPackage.Builder() |
| 148 | + .setPath("/revenue") |
| 149 | + .setSuccessMessage(successMessage) |
| 150 | + .setFailureMessage(failureMessage) |
| 151 | + .setUserAgent(userAgent) |
| 152 | + .addTrackingParameter(PACKAGE_NAME, packageName) |
| 153 | + .addTrackingParameter(MAC_SHORT, macShort) |
| 154 | + .addTrackingParameter(ANDROID_ID, androidId) |
| 155 | + .addTrackingParameter(AMOUNT, amount) |
| 156 | + .addTrackingParameter(EVENT_TOKEN, eventToken) |
| 157 | + .addTrackingParameter(PARAMETERS, paramString) |
| 158 | + .build(); |
| 159 | + getRequestThread().track(revenue); |
| 160 | + } |
| 161 | + |
71 | 162 |
|
72 |
| - RequestTask requestTask = new RequestTask("/revenue"); |
73 |
| - requestTask.setSuccessMessage("Tracked revenue."); |
74 |
| - requestTask.setFailureMessage("Failed to track revenue."); |
75 |
| - requestTask.setUserAgent(userAgent); |
76 |
| - requestTask.execute("app_id", appId, "mac", macShort, "android_id", androidId, "amount", amount, "event_id", eventId, "params", paramString); |
| 163 | + /** |
| 164 | + * Change the verbosity of AdjustIo's logs. |
| 165 | + * |
| 166 | + * @param logLevel The desired minimum log level (default: info) |
| 167 | + * Must be one of the following: |
| 168 | + * - Log.VERBOSE (enable all logging) |
| 169 | + * - Log.DEBUG |
| 170 | + * - Log.INFO (the default) |
| 171 | + * - Log.WARN (disable info logging) |
| 172 | + * - Log.ERROR (disable warnings as well) |
| 173 | + * - Log.ASSERT (disable errors as well) |
| 174 | + */ |
| 175 | + |
| 176 | + public static void setLogLevel(int logLevel) { |
| 177 | + Logger.setLogLevel(logLevel); |
77 | 178 | }
|
78 | 179 |
|
| 180 | + |
79 | 181 | // This line marks the end of the public interface.
|
80 | 182 |
|
81 |
| - private static String appId; |
| 183 | + private static final String PACKAGE_NAME = "app_id"; |
| 184 | + private static final String MAC_SHA1 = "mac_sha1"; |
| 185 | + private static final String MAC_SHORT = "mac"; |
| 186 | + private static final String ANDROID_ID = "android_id"; |
| 187 | + private static final String ATTRIBUTION_ID = "fb_id"; |
| 188 | + private static final String EVENT_TOKEN = "event_id"; |
| 189 | + private static final String PARAMETERS = "params"; |
| 190 | + private static final String AMOUNT = "amount"; |
| 191 | + |
| 192 | + private static String packageName; |
82 | 193 | private static String macSha1;
|
83 | 194 | private static String macShort;
|
84 | 195 | private static String userAgent;
|
85 | 196 | private static String androidId;
|
86 | 197 | private static String attributionId;
|
87 | 198 |
|
88 | 199 | private static void trackSessionStart() {
|
89 |
| - RequestTask requestTask = new RequestTask("/startup"); |
90 |
| - requestTask.setSuccessMessage("Tracked session start."); |
91 |
| - requestTask.setFailureMessage("Failed to track session start."); |
92 |
| - requestTask.setUserAgent(userAgent); |
93 |
| - requestTask.execute("app_id", appId, "mac", macShort, "mac_sha1", macSha1, "android_id", androidId, "fb_id", attributionId); |
| 200 | + TrackingPackage sessionStart = new TrackingPackage.Builder() |
| 201 | + .setPath("/startup") |
| 202 | + .setSuccessMessage("Tracked session start.") |
| 203 | + .setFailureMessage("Failed to track session start.") |
| 204 | + .setUserAgent(userAgent) |
| 205 | + .addTrackingParameter(PACKAGE_NAME, packageName) |
| 206 | + .addTrackingParameter(MAC_SHORT, macShort) |
| 207 | + .addTrackingParameter(MAC_SHA1, macSha1) |
| 208 | + .addTrackingParameter(ANDROID_ID, androidId) |
| 209 | + .addTrackingParameter(ATTRIBUTION_ID, attributionId) |
| 210 | + .build(); |
| 211 | + getRequestThread().track(sessionStart); |
94 | 212 | }
|
95 | 213 |
|
96 |
| - private static void trackSessionEnd() { |
97 |
| - RequestTask requestTask = new RequestTask("/shutdown"); |
98 |
| - requestTask.setSuccessMessage("Tracked session end."); |
99 |
| - requestTask.setFailureMessage("Failed to track session end."); |
100 |
| - requestTask.setUserAgent(userAgent); |
101 |
| - requestTask.execute("app_id", appId, "mac", macShort, "android_id", androidId); |
| 214 | + private static RequestThread requestThread; |
| 215 | + private static RequestThread getRequestThread() { |
| 216 | + if (requestThread == null) { |
| 217 | + requestThread = new RequestThread(); |
| 218 | + } |
| 219 | + return requestThread; |
102 | 220 | }
|
103 | 221 | }
|
0 commit comments