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 org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021
022import org.ametys.web.analytics.WebAnalyticsHelper;
023import org.ametys.web.analytics.WebAnalyticsProvider;
024import org.ametys.web.repository.site.Site;
025import org.ametys.web.repository.site.SiteManager;
026
027/**
028 * Helper which provides web analytics URI building.
029 */
030public class WebAnalyticsXsltHelper implements Serviceable
031{
032    private static WebAnalyticsHelper _webAnalyticsHelper;
033    private static SiteManager _siteManager;
034    
035    @Override
036    public void service(ServiceManager manager) throws ServiceException
037    {
038        _webAnalyticsHelper = (WebAnalyticsHelper) manager.lookup(WebAnalyticsHelper.ROLE);
039        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
040    }
041    
042    /**
043     * Get an event image URI.
044     * @param siteName the site name
045     * @param category the event category.
046     * @param action the event action.
047     * @param label the event label.
048     * @return the event image URI.
049     */
050    public static String eventImageUri(String siteName, String category, String action, String label)
051    {
052        return eventImageUri(siteName, category, action, label, 0);
053    }
054    
055    /**
056     * Get an event image URI.
057     * @param siteName the site name
058     * @param category the event category.
059     * @param action the event action.
060     * @param label the event label.
061     * @param value the event value.
062     * @return the event image URI.
063     */
064    public static String eventImageUri(String siteName, String category, String action, String label, int value)
065    {
066        Site site = _siteManager.getSite(siteName);
067        if (_isNewsletterTrackingEnabled(site))
068        {
069            WebAnalyticsProvider provider = _webAnalyticsHelper.getSelectedProvider(site);
070            return provider.getEventImageUri(site, category, action, label, value, true);
071        }
072        
073        return null;
074    }
075    
076    /**
077     * Get an event link campaign params
078     * @param siteName the site name
079     * @param campaign the event campaign
080     * @param medium the event medium
081     * @param source the event source
082     * @return the event link campaign params
083     */
084    public static String eventLinkCampaignParams(String siteName, String campaign, String medium, String source)
085    {
086        Site site = _siteManager.getSite(siteName);
087        if (_isNewsletterTrackingEnabled(site))
088        {
089            WebAnalyticsProvider provider = _webAnalyticsHelper.getSelectedProvider(site);
090            return provider.getEventLinkCampaignParams(site, campaign, medium, source);
091        }
092        
093        return null;
094    }
095    
096    private static boolean _isNewsletterTrackingEnabled(Site site)
097    {
098        return site.getValue("newsletter-enable-tracking", false, true);
099    }
100}