001/*
002 *  Copyright 2023 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.webanalytics.matomo;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Random;
021
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.core.util.URIUtils;
025import org.ametys.runtime.config.Config;
026import org.ametys.web.analytics.AbstractWebAnalyticsProvider;
027import org.ametys.web.repository.site.Site;
028
029/**
030 * The matomo analytics provider
031 */
032public class MatomoAnalyticsProvider extends AbstractWebAnalyticsProvider
033{
034    private static final Random _RNG = new Random((long) Math.random() * System.nanoTime());
035    
036    public String getEventImageUri(Site site, String category, String action, String label, int value, boolean usePlaceholderTokens)
037    {
038        String matomoUrl = Config.getInstance().getValue(MatomoDataHelper.MATOMO_URL_CONFIG);
039        String siteId = site.getValue(MatomoDataHelper.MATOMO_SITE_ID_SITE_CONFIG);
040        if (StringUtils.isNotBlank(matomoUrl) && StringUtils.isNotBlank(siteId))
041        {
042            Map<String, String> uriParameters = new HashMap<>();
043            uriParameters.put("idsite", siteId);
044            uriParameters.put("rec", "1");
045            uriParameters.put("bots", "1");
046            uriParameters.put("ca", "1");
047            uriParameters.put("e_c", category);
048            uriParameters.put("e_a", action);
049            uriParameters.put("e_n", label);
050            if (value > 0)
051            {
052                uriParameters.put("e_v", String.valueOf(value));
053            }
054            uriParameters.put("rand", usePlaceholderTokens ? RANDOM_NUMBER_TOKEN : String.valueOf(_RNG.nextInt(Integer.MAX_VALUE)));
055            
056            return URIUtils.encodeURI(matomoUrl + "/matomo.php", uriParameters);
057        }
058        
059        return null;
060    }
061    
062    public String getEventLinkCampaignParams(Site site, String campaign, String medium, String source)
063    {
064        StringBuilder linkCampaignParams = new StringBuilder();
065        linkCampaignParams.append("mtm_campaign=");
066        linkCampaignParams.append(campaign);
067        linkCampaignParams.append("&mtm_medium=");
068        linkCampaignParams.append(medium);
069        linkCampaignParams.append("&mtm_source=");
070        linkCampaignParams.append(source);
071        
072        return linkCampaignParams.toString();
073    }
074}