Skip to content
Eason edited this page Jan 16, 2021 · 6 revisions

API

Send Action Beacon

Bundle bundle = new Bundle();
try {
    JSONObject jsonObject = new JSONObject();
    JSONObject inner_json = new JSONObject();
    inner_json.put("inner", "jsonobject");
    jsonObject.put("xxx", inner_json);
    bundle.putString(OmniAnalytics.PayloadType.EVENT_LABEL, jsonObject.toString());
} catch (JSONException e) {
    e.printStackTrace();
}
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.CLICK_MENU);
bundle.putString(OmniAnalytics.PayloadType.EVENT_CATEGORY, "TestCategory");
bundle.putInt(OmniAnalytics.PayloadType.EVENT_VALUE, 123);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Complete Registration

CompleteRegistration completeRegistration = new CompleteRegistration();
completeRegistration.setBirthdayYear("1994");
completeRegistration.setGender(OmniAnalytics.GenderType.MALE);
completeRegistration.setCity("Taipei");
completeRegistration.setRegType("regType");

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.EVENT_CATEGORY, OmniAnalytics.EventCategoryType.COMPLETE_REGISTRATION);
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.COMPLETE_REGISTRATION);
bundle.putSerializable(OmniAnalytics.EventActionType.COMPLETE_REGISTRATION, completeRegistration);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Content Group

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.CONTENT_GROUP, "Home");
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Product Impressions

Impression impression1 = new Impression("12345", "Triblend Android T-Shirt");
impression1.setPrice("15.25");
impression1.setBrand("Google");
impression1.setCategory("Apparel");
impression1.setVariant("Gray");
impression1.setList("Search Results");
impression1.setPosition("1");

Impression impression2 = new Impression("123455", "Same list T-Shirt");
impression2.setPrice("15.25");
impression2.setBrand("Google");
impression2.setCategory("Apparel");
impression2.setVariant("Gray");
impression2.setList("Search Results");
impression2.setPosition("2");

Impression impression3 = new Impression("67890", "Donut Friday Scented T-Shirt");
impression3.setPrice("33.75");
impression3.setBrand("Google");
impression3.setCategory("Apparel");
impression3.setVariant("Black");
impression3.setPosition("1");

ArrayList < Impression > arrayList = new ArrayList < > (Arrays.asList(impression1, impression2, impression3));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.CONTENT_GROUP, "default category list");
bundle.putString(OmniAnalytics.PayloadType.CURRENCY_CODE, "TWD");
bundle.putSerializable(OmniAnalytics.DataType.IMPRESSIONS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.PAGEVIEW, bundle);

Product Click

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.PRODUCT_ACTION_LIST, "Search Results");
bundle.putString(OmniAnalytics.PayloadType.CURRENCY_CODE, "TWD");
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.CLICK_PRODUCT);
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Product Detail Impressions

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.PRODUCT_ACTION_LIST, "category list");
bundle.putString(OmniAnalytics.PayloadType.CURRENCY_CODE, "TWD");
bundle.putSerializable(OmniAnalytics.PayloadType.PRODUCT_ACTION, OmniAnalytics.ActionType.DETAIL);
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.PAGEVIEW, bundle);

Add To Cart

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.CURRENCY_CODE, "TWD");
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.ADD_TO_CART);
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Remove From Cart

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.CURRENCY_CODE, "TWD");
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.REMOVE_FROM_CART);
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Promotion Impressions

Promotion promotion1 = new Promotion("JUNE_PROMO13", "June Sale");
promotion1.setCreative("banner1");
promotion1.setPosition("slot1");

Promotion promotion2 = new Promotion("FREE_SHIP13", "Free Shipping Promo");
promotion1.setCreative("skyscraper1");
promotion1.setPosition("slot2");

ArrayList < Promotion > arrayList = new ArrayList < > (Arrays.asList(promotion1, promotion2));

Bundle bundle = new Bundle();
bundle.putSerializable(OmniAnalytics.DataType.PROMOTIONS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.PAGEVIEW, bundle);

Promotion Clicks

Promotion promotion1 = new Promotion("JUNE_PROMO13", "June Sale");
promotion1.setCreative("banner1");
promotion1.setPosition("slot1");

ArrayList < Promotion > arrayList = new ArrayList < > (Arrays.asList(promotion1));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.CLICK_PROMOTION);
bundle.putSerializable(OmniAnalytics.DataType.PROMOTIONS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.PAGEVIEW, bundle);

Purchases

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");
Product product2 = new Product("product2_name", "product2_id", "12.12", 2);
Product product3 = new Product("product3_name", "product3_id", "15.15", 3);

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1, product2, product3));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.PURCHASE);
bundle.putFloat(OmniAnalytics.PayloadType.REVENUE, 20.20 f);
bundle.putString(OmniAnalytics.PayloadType.TRANSACTION_ID, "ABC-12345");
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Refunds

Full Refunds

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.REFUND);
bundle.putString(OmniAnalytics.PayloadType.TRANSACTION_ID, "ABC");
bundle.putString(OmniAnalytics.PayloadType.PRODUCT_ACTION, "custom_product_action");
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Partial Refunds

Product product1 = new Product("product1_name", "product1_id", "10.10", 1);
product1.setBrand("Omni_Brand");
product1.setCategory("Omni_Category");
product1.setVariant("Omni_Variant");
product1.setCoupon("Omni_Coupon");
Product product2 = new Product("product2_name", "product2_id", "12.12", 2);
Product product3 = new Product("product3_name", "product3_id", "15.15", 3);

ArrayList < Product > arrayList = new ArrayList < > (Arrays.asList(product1, product2, product3));

Bundle bundle = new Bundle();
bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.REFUND);
bundle.putString(OmniAnalytics.PayloadType.TRANSACTION_ID, "ABC");
bundle.putString(OmniAnalytics.PayloadType.PRODUCT_ACTION, "custom_product_action");
bundle.putSerializable(OmniAnalytics.DataType.PRODUCTS, arrayList);
OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);

Checkout

Spinner spCheckout = findViewById(R.id.spCheckout);
final String[] checkoutArray = new String[] {
    "Choose One", "ATM", "Credit Card"
};
ArrayAdapter < String > adapter = new ArrayAdapter < > (this, android.R.layout.simple_spinner_item, checkoutArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCheckout.setAdapter(adapter);
spCheckout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView < ? > parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString(OmniAnalytics.PayloadType.EVENT_ACTION, OmniAnalytics.EventActionType.CLICK_CHECKOUT);
        bundle.putInt(OmniAnalytics.PayloadType.CHECKOUT_STEP, position);
        bundle.putString(OmniAnalytics.PayloadType.CHECKOUT_STEP_OPTION, checkoutArray[position]);
        OmniAnalytics.getInstance(MainActivity.this).logEvent(OmniAnalytics.HitType.EVENT, bundle);
    }

    @Override
    public void onNothingSelected(AdapterView < ? > parent) {

    }
});

Tracking open notification without OmniApplication

in your FirebaseMessagingService’s onMessageReceived, use OmniAnalytics.getInstance(this).processRemoteMessage(remoteMessage) to get Intent object.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    String TAG = this.getClass().getName();
    Bitmap bitmap;

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage remoteMessage) {
        // call OmniAnalytics.getInstance(this).processRemoteMessage() to get Intent object
        Intent intent = OmniAnalytics.getInstance(this).processRemoteMessage(remoteMessage);
        intent.setClass(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        String title = getResources().getString(R.string.app_name);
        String body = null;
        if (remoteMessage.getNotification() != null){
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        }

        if (remoteMessage.getData().size() > 0){
            intent.putExtra("document_id",remoteMessage.getData().get("document_id"));
            if (remoteMessage.getData().get("text") != null) {
                body =remoteMessage.getData().get("text");
            }
        }
        if (body == null){
            return;
        }

        String imageUri = remoteMessage.getData().get("image");
        bitmap = getBitmapfromUrl(imageUri);

        String extras = "";
        for (String key : remoteMessage.getData().keySet()) {
            extras+="key:"+key+", value:"+remoteMessage.getData().get(key)+"\n";
            Log.d(TAG, "sendNotification: "+key+", value="+remoteMessage.getData().get(key));
        }

        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, (int)(Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        String channelId = "channel01";
        int notifyId = (int) System.currentTimeMillis();
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setLargeIcon(bitmap)
                        .setContentTitle(title)
                        .setContentText(body)
                        .setStyle(new NotificationCompat.BigPictureStyle().setSummaryText("SummartText" + title +"\n"+extras).bigPicture(bitmap))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("bigText"+ body +"\n"+extras))
                        .setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setColor(ContextCompat.getColor(this, android.R.color.transparent))
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "TestGA notification",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(notifyId /* ID of notification */, notificationBuilder.build());
    }
    /*
     *To get a Bitmap image from the URL received
     * */
    public Bitmap getBitmapfromUrl(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(input);
            return bitmap;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;

        }
    }

}

call OmniAnalytics.getInstance(this).processIntent(intent) when you get intent object

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    OmniAnalytics.getInstance(this).processIntent(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    OmniAnalytics.setTrackingId("your tracking id;
    OmniAnalytics.getInstance(this).processIntent(getIntent());
}