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;
}
Aggregations