001/*
002 *  Copyright 2025 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.tracking;
017
018import java.util.List;
019
020import org.apache.avalon.framework.configuration.Configurable;
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.matomo.java.tracking.MatomoRequest;
024import org.matomo.java.tracking.MatomoRequests;
025
026import org.ametys.plugins.webanalytics.matomo.MatomoDataHelper;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028import org.ametys.web.repository.site.Site;
029
030/**
031 * Abstract class for Matomo tracking provider. It provide only {#MatomoRequests.event}
032 */
033public abstract class AbstractMatomoEventTrackingProvider extends AbstractLogEnabled implements MatomoTrackingProvider, Configurable
034{
035    /** The events category */
036    protected String _eventsCategory;
037    
038    /** The events category */
039    protected String _eventsAction;
040    
041    public void configure(Configuration configuration) throws ConfigurationException
042    {
043        _eventsCategory = configuration.getChild("category").getValue();
044        _eventsAction = configuration.getChild("action").getValue();
045    }
046    
047    public List<MatomoRequest> getRequests(Site site)
048    {
049        String matomoSiteId = site.getValue(MatomoDataHelper.MATOMO_SITE_ID_SITE_CONFIG);
050
051        return getEvents(site).stream()
052                .map(e -> _getRequest(site, matomoSiteId, e))
053                .toList();
054    }
055    
056    private MatomoRequest _getRequest(Site site, String matomoSiteId, MatomoEvent event)
057    {
058        if (getLogger().isInfoEnabled())
059        {
060            getLogger().info("Sending request to Matomo: idsite={}&e_c={}&e_a={}&e_n={}&e_v={}", matomoSiteId, getEventsCategory(), getEventsAction(), event.name(), event.count());
061        }
062
063        return MatomoRequests.event(getEventsCategory(), getEventsAction(), event.name(), event.count() > 0 ? event.count() : null)
064                    .siteId(Integer.valueOf(matomoSiteId))
065                    .userId("--AmetysServer--") // Use a invented user to regroup all tracking server events
066                    .actionUrl(site.getUrl() + "/tracking-url") // Dummy URL to have a valid request
067                    .build();
068    }
069    
070    /**
071     * The events category
072     * @return the events category
073     */
074    protected String getEventsCategory()
075    {
076        return _eventsCategory;
077    }
078    
079    /**
080     * The events action
081     * @return the events action
082     */
083    protected String getEventsAction()
084    {
085        return _eventsAction;
086    }
087    
088    /**
089     * Get the list of event to send to matomo
090     * @param site the site
091     * @return the list of matomo events
092     */
093    abstract protected List<MatomoEvent> getEvents(Site site);
094    
095    /**
096     * Record for a matomo event
097     * @param name the name of the event
098     * @param count the count value of the event
099     */
100    public record MatomoEvent(String name, double count) { /* empty */ }
101}
102