All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adjust.sdk.plugin.AdjustTrademob Maven / Gradle / Ivy

There is a newer version: 4.38.5
Show newest version
package com.adjust.sdk.plugin;


import com.adjust.sdk.AdjustEvent;
import com.adjust.sdk.AdjustFactory;
import com.adjust.sdk.ILogger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;


public class AdjustTrademob {

    private static int MAX_LISTING_ITEMS_COUNT = 5;

    private static ILogger logger = AdjustFactory.getLogger();

    public static void injectViewItemIntoEvent(AdjustEvent event, String itemId, Map metadata) {
        event.addPartnerParameter("tm_item", itemId);
        String jsonMetadata = stringifyMetadata(metadata);
        event.addPartnerParameter("tm_md", jsonMetadata);
    }

    public static void injectViewListingIntoEvent(AdjustEvent event, List itemIds, Map metadata) {
        String jsonItems = stringifyItemIds(itemIds);
        event.addPartnerParameter("tm_item", jsonItems);
        String jsonMetadata = stringifyMetadata(metadata);
        event.addPartnerParameter("tm_md", jsonMetadata);
    }

    public static void injectAddToBasketIntoEvent(AdjustEvent event, List items,  Map metadata) {
        String jsonMetadata = stringifyMetadata(metadata);
        event.addPartnerParameter("tm_md", jsonMetadata);
        String jsonItems = stringifyItems(items);
        event.addPartnerParameter("tm_item", jsonItems);
    }

    public static void injectCheckoutIntoEvent(AdjustEvent event, List items, Map metadata) {
        String jsonMetadata = stringifyMetadata(metadata);
        event.addPartnerParameter("tm_md", jsonMetadata);
        String jsonItems = stringifyItems(items);
        event.addPartnerParameter("tm_item", jsonItems);
    }

    private static String stringifyItemIds(List itemIds) {
        if (itemIds == null) {
            logger.warn("TM View Listing item ids list is null. Empty ids array will be sent.");
            itemIds = new ArrayList();
        }
        StringBuffer tmViewList = new StringBuffer("[");
        int itemsSize = itemIds.size();
        int i = 0;

        while (i < itemsSize) {
            String itemId = itemIds.get(i);
            String itemString = String.format(Locale.US, "\"%s\"", itemId);
            tmViewList.append(itemString);

            i++;

            if (i == itemsSize ||  i >= MAX_LISTING_ITEMS_COUNT) {
                break;
            }

            tmViewList.append(",");
        }

        tmViewList.append("]");

        return tmViewList.toString();
    }

    private static String stringifyItems(List items) {
        if (items == null) {
            logger.warn("TM View Listing item ids list is empty. Empty items will be sent.");
            items = new ArrayList();
        }

        StringBuffer itemsStrBuffer = new StringBuffer("[");
        int itemsSize = items.size();

        for (int i = 0; i < itemsSize; ) {
            TrademobItem item = items.get(i);
            String itemString = String.format(Locale.US, "{\"id\":\"%s\",\"price\":%f,\"quantity\":%d}",
                    item.itemId,
                    item.price,
                    item.quantity);
            itemsStrBuffer.append(itemString);

            i++;

            if (i == itemsSize ||  i >= MAX_LISTING_ITEMS_COUNT) {
                break;
            }

            itemsStrBuffer.append(",");
        }

        itemsStrBuffer.append("]");

        return itemsStrBuffer.toString();
    }

    private static String stringifyMetadata(Map metadata) {

        if(null == metadata) {
            return "{}";
        }
        String res = "{";

        Iterator> iterator = metadata.entrySet().iterator();

        while(iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = "\"".concat(entry.getKey()).concat("\"");
            String value = "\"".concat(entry.getValue()).concat("\"");
            res = res.concat(key).concat(":").concat(value);

            if (iterator.hasNext()) {
                res = res.concat(",");
            }
        }

        return res.concat("}");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy