package it.acxent.gtm; import it.acxent.art.Articolo; import it.acxent.art.ArticoloTaglia; import it.acxent.art.ArticoloVariante; import it.acxent.cart.Cart; import it.acxent.cart.CartItem; import it.acxent.contab.Documento; import it.acxent.contab.RigaDocumento; import it.acxent.util.Vectumerator; import java.util.Enumeration; import org.json.JSONArray; import org.json.JSONObject; public class GoogleDataLayerBuilder { public static final long TIPO_EVENTO_DETTAGLIO_PRODOTTO_0 = 0L; public static final long TIPO_EVENTO_ADD_ITEM_1 = 1L; public static final long TIPO_EVENTO_DEL_ITEM_2 = 2L; public static final long TIPO_EVENTO_DELETE_CART_3 = 3L; public static final long TIPO_EVENTO_CHECKOUT_NEW_USER_4 = 10L; public static final long TIPO_EVENTO_CHECKOUT_STEP1_VEDI_CARRELLO_11 = 11L; public static final long TIPO_EVENTO_CHECKOUT_STEP2_PAGINA_LOGIN_12 = 12L; public static final long TIPO_EVENTO_CHECKOUT_STEP3_REGISTRAZIONE_ORDINE_13 = 13L; public static final long TIPO_EVENTO_CHECKOUT_STEP4_INIZIO_PAGAMENTO_14 = 14L; public static final long TIPO_EVENTO_PURCHASE_99 = 99L; private static final boolean debug = false; private long tipoEvento; private String evento; public static String getDettaglioProdotto(String evento, ArticoloVariante bean) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jdetail = new JSONObject(); JSONArray jproducts = new JSONArray(); JSONObject jrow = new JSONObject(); jrow.put("name", bean.getDescrizione()); jrow.put("id", bean.getCodiceVariante()); jrow.put("category", bean.getTipo().getDescrizioneCompletaHypen("it")); jproducts.put(jrow); jdetail.put("products", jproducts); jecommerce.put("detail", jdetail); jo.put("ecommerce", jecommerce); return jo.toString(); } public static String getDettaglioProdotto(String evento, Articolo bean) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jdetail = new JSONObject(); JSONArray jproducts = new JSONArray(); JSONObject jrow = new JSONObject(); if (bean.getId_articoloVariante() > 0L) { jrow.put("name", bean.getArticoloVariante().getDescrizioneCompleta()); jrow.put("id", bean.getArticoloVariante().getCodiceVariante()); } else { jrow.put("name", bean.getDescrizioneCompleta()); jrow.put("id", bean.getCodice()); } jrow.put("category", bean.getTipo().getDescrizioneCompletaHypen("it")); jproducts.put(jrow); jdetail.put("products", jproducts); jecommerce.put("detail", jdetail); jo.put("ecommerce", jecommerce); return jo.toString(); } public static String addItemToCart(String evento, CartItem bean) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jadd = new JSONObject(); JSONArray jproducts = new JSONArray(); JSONObject jrow = new JSONObject(); jrow.put("name", bean.getDbItem().getDescrizione()); if (bean.getDbItem() instanceof ArticoloTaglia) { jrow.put("id", ((ArticoloTaglia)bean.getDbItem()).getCodiceAT()); } else if (bean.getDbItem() instanceof ArticoloVariante) { jrow.put("id", ((ArticoloVariante)bean.getDbItem()).getCodiceVariante()); } else if (bean.getDbItem() instanceof Articolo) { jrow.put("id", ((Articolo)bean.getDbItem()).getCodice()); } else { jrow.put("id", "?? errore??"); } jrow.put("quantity", bean.getQuantity()); jproducts.put(jrow); jadd.put("products", jproducts); jecommerce.put("add", jadd); jo.put("ecommerce", jecommerce); return jo.toString(); } public static String showCart(String evento, String step, Cart cart) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jCheckout = new JSONObject(); JSONObject jActionField = new JSONObject(); JSONArray jproducts = new JSONArray(); JSONObject jrow = new JSONObject(); Enumeration enu = cart.getItems(); String lang = "it"; while (enu.hasMoreElements()) { CartItem ci = enu.nextElement(); if (ci.getQuantity() > 0.0D) { jrow.put("name", ci.getDbItem().getDescrizione()); if (ci.getDbItem() instanceof ArticoloTaglia) { jrow.put("id", ((ArticoloTaglia)ci.getDbItem()).getCodiceAT()); } else if (ci.getDbItem() instanceof ArticoloVariante) { jrow.put("id", ((ArticoloVariante)ci.getDbItem()).getCodiceVariante()); } else if (ci.getDbItem() instanceof Articolo) { jrow.put("id", ((Articolo)ci.getDbItem()).getCodice()); } else { jrow.put("id", "?? errore??"); } jrow.put("quantity", ci.getQuantity()); jproducts.put(jrow); } } jActionField.put("step", step); jCheckout.put("actionField", jActionField); jCheckout.put("products", jproducts); jecommerce.put("checkout", jCheckout); jo.put("ecommerce", jecommerce); return jo.toString(); } public static String eventStep(String evento, String step) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jCheckout = new JSONObject(); JSONObject jActionField = new JSONObject(); jActionField.put("step", step); jCheckout.put("actionField", jActionField); jecommerce.put("checkout", jCheckout); jo.put("ecommerce", jecommerce); return jo.toString(); } public static String purchaseOrder(String evento, Documento doc) { JSONObject jo = new JSONObject(); jo.put("event", evento); JSONObject jecommerce = new JSONObject(); JSONObject jPurchase = new JSONObject(); JSONObject jActionField = new JSONObject(); JSONArray jproducts = new JSONArray(); JSONObject jrow = new JSONObject(); Vectumerator vec = doc.findRigheDocumento(0, 0, 0); String lang = "it"; while (vec.hasMoreElements()) { RigaDocumento row = (RigaDocumento)vec.nextElement(); jrow.put("id", row.getDescrizioneCodiceRiga()); jrow.put("name", row.getDescrizioneRiga()); jrow.put("quantity", row.getQuantita()); jrow.put("price", doc.getNf().format(row.getTotImportoRigaConSconto())); jrow.put("category", row.getArticolo().getTipo().getDescrizioneCompletaSpaces("it")); jproducts.put(jrow); } jActionField.put("id", doc.getNumeroDocumentoCompleto()); jActionField.put("revenue", doc.getNf().format(doc.getTotaleDocumento())); jActionField.put("tax", doc.getNf().format(doc.getImportoIvaTotale())); jActionField.put("shipping", doc.getNf().format(doc.getTotaleAltriCostiConIva())); jActionField.put("coupon", doc.getNotePagamento()); jPurchase.put("actionField", jActionField); jPurchase.put("products", jproducts); jecommerce.put("purchase", jPurchase); jo.put("ecommerce", jecommerce); return jo.toString(); } }