-
Notifications
You must be signed in to change notification settings - Fork 403
/
MainActivity.kt
301 lines (260 loc) · 12.3 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.google.firebase.example.analytics.kotlin
import android.os.Bundle
import android.os.Parcelable
import androidx.appcompat.app.AppCompatActivity
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdRevenueListener
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.analytics
import com.google.firebase.analytics.logEvent
import com.google.firebase.example.analytics.R
import com.google.firebase.Firebase
import com.ironsource.mediationsdk.impressionData.ImpressionData
import com.ironsource.mediationsdk.impressionData.ImpressionDataListener
class MainActivity :
AppCompatActivity(),
// importing libraries to support 3rd party ad_impression snippets
MaxAdRevenueListener,
ImpressionDataListener {
// [START declare_analytics]
private lateinit var firebaseAnalytics: FirebaseAnalytics
// [END declare_analytics]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// [START shared_app_measurement]
// Obtain the FirebaseAnalytics instance.
firebaseAnalytics = Firebase.analytics
// [END shared_app_measurement]
enhancedEcommerce()
setUserFavoriteFood("pizza")
recordImageView()
logCustomEvent()
}
public override fun onResume() {
super.onResume()
recordScreenView()
}
fun enhancedEcommerce() {
// [START create_items]
val itemJeggings = Bundle().apply {
putString(FirebaseAnalytics.Param.ITEM_ID, "SKU_123")
putString(FirebaseAnalytics.Param.ITEM_NAME, "jeggings")
putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "pants")
putString(FirebaseAnalytics.Param.ITEM_VARIANT, "black")
putString(FirebaseAnalytics.Param.ITEM_BRAND, "Google")
putDouble(FirebaseAnalytics.Param.PRICE, 9.99)
}
val itemBoots = Bundle().apply {
putString(FirebaseAnalytics.Param.ITEM_ID, "SKU_456")
putString(FirebaseAnalytics.Param.ITEM_NAME, "boots")
putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "shoes")
putString(FirebaseAnalytics.Param.ITEM_VARIANT, "brown")
putString(FirebaseAnalytics.Param.ITEM_BRAND, "Google")
putDouble(FirebaseAnalytics.Param.PRICE, 24.99)
}
val itemSocks = Bundle().apply {
putString(FirebaseAnalytics.Param.ITEM_ID, "SKU_789")
putString(FirebaseAnalytics.Param.ITEM_NAME, "ankle_socks")
putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "socks")
putString(FirebaseAnalytics.Param.ITEM_VARIANT, "red")
putString(FirebaseAnalytics.Param.ITEM_BRAND, "Google")
putDouble(FirebaseAnalytics.Param.PRICE, 5.99)
}
// [END create_items]
// [START view_item_list]
val itemJeggingsWithIndex = Bundle(itemJeggings).apply {
putLong(FirebaseAnalytics.Param.INDEX, 1)
}
val itemBootsWithIndex = Bundle(itemBoots).apply {
putLong(FirebaseAnalytics.Param.INDEX, 2)
}
val itemSocksWithIndex = Bundle(itemSocks).apply {
putLong(FirebaseAnalytics.Param.INDEX, 3)
}
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM_LIST) {
param(FirebaseAnalytics.Param.ITEM_LIST_ID, "L001")
param(FirebaseAnalytics.Param.ITEM_LIST_NAME, "Related products")
param(
FirebaseAnalytics.Param.ITEMS,
arrayOf(itemJeggingsWithIndex, itemBootsWithIndex, itemSocksWithIndex),
)
}
// [END view_item_list]
// [START select_item]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM) {
param(FirebaseAnalytics.Param.ITEM_LIST_ID, "L001")
param(FirebaseAnalytics.Param.ITEM_LIST_NAME, "Related products")
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggings))
}
// [END select_item]
// [START view_product_details]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 9.99)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggings))
}
// [END view_product_details]
// [START add_to_cart_wishlist]
val itemJeggingsWishlist = Bundle(itemJeggings).apply {
putLong(FirebaseAnalytics.Param.QUANTITY, 2)
}
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ADD_TO_WISHLIST) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 2 * 9.99)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsWishlist))
}
// [END add_to_cart_wishlist]
// [START view_cart]
val itemJeggingsCart = Bundle(itemJeggings).apply {
putLong(FirebaseAnalytics.Param.QUANTITY, 2)
}
val itemBootsCart = Bundle(itemBoots).apply {
putLong(FirebaseAnalytics.Param.QUANTITY, 1)
}
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_CART) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 2 * 9.99 + 1 * 24.99)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsCart, itemBootsCart))
}
// [END view_cart]
// [START remove_from_cart]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.REMOVE_FROM_CART) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 1 * 24.99)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemBootsCart))
}
// [END remove_from_cart]
// [START start_checkout]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.BEGIN_CHECKOUT) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 14.98)
param(FirebaseAnalytics.Param.COUPON, "SUMMER_FUN")
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsCart))
}
// [END start_checkout]
// [START add_shipping]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ADD_SHIPPING_INFO) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 14.98)
param(FirebaseAnalytics.Param.COUPON, "SUMMER_FUN")
param(FirebaseAnalytics.Param.SHIPPING_TIER, "Ground")
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsCart))
}
// [END add_shipping]
// [START add_payment]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ADD_PAYMENT_INFO) {
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 14.98)
param(FirebaseAnalytics.Param.COUPON, "SUMMER_FUN")
param(FirebaseAnalytics.Param.PAYMENT_TYPE, "Visa")
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsCart))
}
// [END add_payment]
// [START log_purchase]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE) {
param(FirebaseAnalytics.Param.TRANSACTION_ID, "T12345")
param(FirebaseAnalytics.Param.AFFILIATION, "Google Store")
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 14.98)
param(FirebaseAnalytics.Param.TAX, 2.58)
param(FirebaseAnalytics.Param.SHIPPING, 5.34)
param(FirebaseAnalytics.Param.COUPON, "SUMMER_FUN")
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggingsCart))
}
// [END log_purchase]
// [START log_refund]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.REFUND) {
param(FirebaseAnalytics.Param.TRANSACTION_ID, "T12345")
param(FirebaseAnalytics.Param.AFFILIATION, "Google Store")
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, 9.99)
// (Optional) for partial refunds, define the item ID and quantity of refunded items
param(FirebaseAnalytics.Param.ITEM_ID, "SKU_123")
param(FirebaseAnalytics.Param.QUANTITY, 1)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemJeggings))
}
// [END log_refund]
// [START apply_promo]
val promoParams = Bundle().apply {
putString(FirebaseAnalytics.Param.PROMOTION_ID, "SUMMER_FUN")
putString(FirebaseAnalytics.Param.PROMOTION_NAME, "Summer Sale")
putString(FirebaseAnalytics.Param.CREATIVE_NAME, "summer2020_promo.jpg")
putString(FirebaseAnalytics.Param.CREATIVE_SLOT, "featured_app_1")
putString(FirebaseAnalytics.Param.LOCATION_ID, "HERO_BANNER")
putParcelableArray(FirebaseAnalytics.Param.ITEMS, arrayOf<Parcelable>(itemJeggings))
}
// Promotion displayed
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_PROMOTION, promoParams)
// Promotion selected
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_PROMOTION, promoParams)
// [END apply_promo]
}
private fun setUserFavoriteFood(food: String) {
// [START user_property]
firebaseAnalytics.setUserProperty("favorite_food", food)
// [END user_property]
}
private fun recordImageView() {
val id = "imageId"
val name = "imageTitle"
// [START image_view_event]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM) {
param(FirebaseAnalytics.Param.ITEM_ID, id)
param(FirebaseAnalytics.Param.ITEM_NAME, name)
param(FirebaseAnalytics.Param.CONTENT_TYPE, "image")
}
// [END image_view_event]
}
private fun recordScreenView() {
val screenName = "Home Page"
// [START set_current_screen]
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
param(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
param(FirebaseAnalytics.Param.SCREEN_CLASS, "MainActivity")
}
// [END set_current_screen]
}
private fun logCustomEvent() {
val name = "customImage"
val text = "I'd love to hear more about $name"
// [START custom_event]
firebaseAnalytics.logEvent("share_image") {
param("image_name", name)
param("full_text", text)
}
// [END custom_event]
}
// [START ad_impression_applovin]
override fun onAdRevenuePaid(impressionData: MaxAd?) {
impressionData?.let {
firebaseAnalytics = Firebase.analytics
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION) {
param(FirebaseAnalytics.Param.AD_PLATFORM, "appLovin")
param(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.adUnitId)
param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.format.label)
param(FirebaseAnalytics.Param.AD_SOURCE, impressionData.networkName)
param(FirebaseAnalytics.Param.VALUE, impressionData.revenue)
param(FirebaseAnalytics.Param.CURRENCY, "USD") // All Applovin revenue is sent in USD
}
}
}
// [END ad_impression_applovin]
// [START ad_impression_ironsource]
override fun onImpressionSuccess(impressionData: ImpressionData) {
// The onImpressionSuccess will be reported when the rewarded video and interstitial ad is
// opened.
// For banners, the impression is reported on load success. Log.d(TAG, "onImpressionSuccess" +
// impressionData)
firebaseAnalytics = Firebase.analytics
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION) {
param(FirebaseAnalytics.Param.AD_PLATFORM, "ironSource")
param(FirebaseAnalytics.Param.AD_SOURCE, impressionData.adNetwork)
param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.adUnit)
param(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.instanceName)
param(FirebaseAnalytics.Param.CURRENCY, "USD")
param(FirebaseAnalytics.Param.VALUE, impressionData.revenue)
}
}
// [END ad_impression_ironsource]
}