Search in sources :

Example 1 with BroadleafTemplateNonVoidElement

use of org.broadleafcommerce.presentation.model.BroadleafTemplateNonVoidElement in project BroadleafCommerce by BroadleafCommerce.

the class GoogleUniversalAnalyticsProcessor method getReplacementModel.

@Override
public BroadleafTemplateModel getReplacementModel(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    StringBuffer sb = new StringBuffer();
    Map<String, String> trackers = getTrackers();
    if (MapUtils.isNotEmpty(trackers)) {
        sb.append("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
        sb.append("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
        sb.append("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
        sb.append("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
        String orderNumberExpression = tagAttributes.get("ordernumber");
        String orderNumber = null;
        if (orderNumberExpression != null) {
            orderNumber = (String) context.parseExpression(orderNumberExpression);
        }
        Order order = null;
        if (orderNumber != null) {
            order = orderService.findOrderByOrderNumber(orderNumber);
        }
        for (Entry<String, String> tracker : trackers.entrySet()) {
            String trackerName = tracker.getKey();
            String trackerPrefix = "";
            String id = tracker.getValue();
            sb.append("ga('create', '" + id + "', 'auto', {");
            if (!"webProperty".equals(trackerName)) {
                trackerPrefix = trackerName + ".";
                sb.append("'name': '" + trackerName + "'");
                if (testLocal) {
                    sb.append(",");
                }
            }
            if (testLocal) {
                sb.append("'cookieDomain': 'none'");
            }
            sb.append("});");
            if ("webProperty".equals(trackerName)) {
                HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
                if (request != null) {
                    Map<String, String> setValuesMap = (Map<String, String>) request.getAttribute("blGAValuesMap");
                    if (setValuesMap != null) {
                        for (Map.Entry<String, String> entry : setValuesMap.entrySet()) {
                            sb.append("ga('" + trackerPrefix + "set',").append(entry.getKey()).append(",").append(entry.getValue()).append(");");
                        }
                    }
                }
            }
            sb.append("ga('" + trackerPrefix + "send', 'pageview');");
            if (isIncludeLinkAttribution()) {
                sb.append(getLinkAttributionJs(trackerPrefix));
            }
            if (isIncludeDisplayAdvertising()) {
                sb.append(getDisplayAdvertisingJs(trackerPrefix));
            }
            if (order != null) {
                sb.append(getTransactionJs(order, trackerPrefix));
            }
        }
        // Add contentNode to the document
        BroadleafTemplateModel model = context.createModel();
        BroadleafTemplateNonVoidElement scriptTag = context.createNonVoidElement("script");
        BroadleafTemplateElement script = context.createTextElement(sb.toString());
        scriptTag.addChild(script);
        model.addElement(scriptTag);
        return model;
    } else {
        LOG.warn("No trackers were found, not outputting Google Analytics script. Set the googleAnalytics.webPropertyId" + " and/or the googleAnalytics.masterWebPropertyId system properties to output Google Analytics");
    }
    return null;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HttpServletRequest(javax.servlet.http.HttpServletRequest) BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) BroadleafTemplateNonVoidElement(org.broadleafcommerce.presentation.model.BroadleafTemplateNonVoidElement)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Order (org.broadleafcommerce.core.order.domain.Order)1 BroadleafTemplateElement (org.broadleafcommerce.presentation.model.BroadleafTemplateElement)1 BroadleafTemplateModel (org.broadleafcommerce.presentation.model.BroadleafTemplateModel)1 BroadleafTemplateNonVoidElement (org.broadleafcommerce.presentation.model.BroadleafTemplateNonVoidElement)1