001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.newsletter.analytics; 017 018import java.util.Optional; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023import org.apache.commons.lang3.StringUtils; 024 025import org.ametys.web.analytics.WebAnalyticsProvider; 026import org.ametys.web.analytics.WebAnalyticsProviderExtensionPoint; 027import org.ametys.web.repository.site.Site; 028import org.ametys.web.repository.site.SiteManager; 029 030/** 031 * Helper which provides web analytics URI building. 032 */ 033public class WebAnalyticsXsltHelper implements Serviceable 034{ 035 private static WebAnalyticsProviderExtensionPoint _webAnalyticsProviderEP; 036 private static SiteManager _siteManager; 037 038 @Override 039 public void service(ServiceManager manager) throws ServiceException 040 { 041 _webAnalyticsProviderEP = (WebAnalyticsProviderExtensionPoint) manager.lookup(WebAnalyticsProviderExtensionPoint.ROLE); 042 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 043 } 044 045 /** 046 * Get an event image URI. 047 * @param siteName the site name 048 * @param category the event category. 049 * @param action the event action. 050 * @param label the event label. 051 * @return the event image URI. 052 */ 053 public static String eventImageUri(String siteName, String category, String action, String label) 054 { 055 return eventImageUri(siteName, category, action, label, 0); 056 } 057 058 /** 059 * Get an event image URI. 060 * @param siteName the site name 061 * @param category the event category. 062 * @param action the event action. 063 * @param label the event label. 064 * @param value the event value. 065 * @return the event image URI. 066 */ 067 public static String eventImageUri(String siteName, String category, String action, String label, int value) 068 { 069 Site site = _siteManager.getSite(siteName); 070 if (_isNewsletterTrackingEnabled(site)) 071 { 072 Optional<WebAnalyticsProvider> provider = _getCurrentProvider(site); 073 if (provider.isPresent()) 074 { 075 return provider.get().getEventImageUri(site, category, action, label, value, true); 076 } 077 } 078 079 return null; 080 } 081 082 /** 083 * Get an event link campaign params 084 * @param siteName the site name 085 * @param campaign the event campaign 086 * @param medium the event medium 087 * @param source the event source 088 * @return the event link campaign params 089 */ 090 public static String eventLinkCampaignParams(String siteName, String campaign, String medium, String source) 091 { 092 Site site = _siteManager.getSite(siteName); 093 if (_isNewsletterTrackingEnabled(site)) 094 { 095 Optional<WebAnalyticsProvider> provider = _getCurrentProvider(site); 096 if (provider.isPresent()) 097 { 098 return provider.get().getEventLinkCampaignParams(site, campaign, medium, source); 099 } 100 } 101 102 return null; 103 } 104 105 private static boolean _isNewsletterTrackingEnabled(Site site) 106 { 107 return site.getValue("newsletter-enable-tracking", false, true); 108 } 109 110 private static Optional<WebAnalyticsProvider> _getCurrentProvider(Site site) 111 { 112 String trackingProviderId = site.getValue("tracking-provider", true, StringUtils.EMPTY); 113 if (_webAnalyticsProviderEP.hasExtension(trackingProviderId)) 114 { 115 return Optional.of(_webAnalyticsProviderEP.getExtension(trackingProviderId)); 116 } 117 else 118 { 119 return Optional.empty(); 120 } 121 } 122}