<?php
// +-------------------------------------------------+
// | 2002-2007 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: analytics_service_eulerian.class.php,v 1.5 2026/03/20 10:12:15 dgoron Exp $

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

class analytics_service_eulerian {
	
	public static function get_label() {
		return "Eulerian";
	}
	
	public static function get_parameters_content_form($parameters=array()) {
		
		return '
		<div class="row">
			<label class="etiquette" for="analytics_service_parameters_domain">domain</label>
		</div>
		<div class="row">
			<input type="text" class="saisie-40em" id="analytics_service_parameters_domain" name="analytics_service_parameters[domain]" value="'.(!empty($parameters['domain']) ? $parameters['domain'] : '').'" />
		</div>';

	}
	
	public static function get_default_template() {
		return "
        <!-- Tag standard - Eulerian Analytics  -->
        <script>
            (function(e,a){var i=e.length,y=5381,k='script',s=window,v=document,o=v.createElement(k);for(;i;){i-=1;y=(y*33)^e.charCodeAt(i)}y='_EA_'+(y>>>=0);(function(e,a,s,y){s[a]=s[a]||function(){(s[y]=s[y]||[]).push(arguments);s[y].eah=e;};}(e,a,s,y));i=new Date/1E7|0;o.ea=y;y=i%26;o.async=1;o.src='//'+e+'/'+String.fromCharCode(97+y,122-y,65+y)+(i%1E3)+'.js?2';s=v.getElementsByTagName(k)[0];s.parentNode.insertBefore(o,s);})
            ('{{ domain }}','EA_push');
        </script>
        <script>
            (function() {
                var EA_datalayer = [];

                /* ========= Identification page ========= */
                let pageGroupValue = '{{page.type_page | escape }} : {{page.subtype_page | escape }}';
                if (pageGroupValue) {
                    EA_datalayer.push('pagegroup');
                    EA_datalayer.push(reverse_html_entities(pageGroupValue));
                }
                let pageNameValue = '{{page.title | escape}}';
                if (pageNameValue) {
                    EA_datalayer.push('page_name');
                    EA_datalayer.push(reverse_html_entities(pageNameValue));
                }
                let h1 = document.querySelector('h1');
                let titre = h1 ? h1.innerText : '{{page.title | escape}}';
                EA_datalayer.push('pagelabel');
                EA_datalayer.push(reverse_html_entities(titre));
                EA_datalayer.push('path');
                EA_datalayer.push(reverse_html_entities(titre));

                /* ========= Usager connecte oui/non ========= */
                EA_datalayer.push('uid');
                EA_datalayer.push('{% if session.vars.id_empr %}{{session_vars.id_empr}}{% endif %}');

                /* ========= Tracking provenance newsletter ========= */
                const params = new URLSearchParams(window.location.search);
                if (params.get('utm_source')) {
                    EA_datalayer.push('utm_source');
                    EA_datalayer.push(params.get('utm_source'));
                    if (params.get('utm_medium')) {
                        EA_datalayer.push('utm_medium');
                        EA_datalayer.push(params.get('utm_medium'));
                    }
                    if (params.get('utm_campaign')) {
                        EA_datalayer.push('utm_campaign');
                        EA_datalayer.push(params.get('utm_campaign'));
                    }
                }

                /* ========= Moteur de recherche interne ========= */
                let fromSearch = false;
                let searchHumanQuery = document.querySelector('#search-human-query');
                if (searchHumanQuery) {
                    EA_datalayer.push('isearchengine');
                    EA_datalayer.push(searchHumanQuery.innerText);
                    fromSearch = true;
                } else {
                    let searchSegmentHumanQuery = document.querySelector('#segment_search_human_query');
                    if (searchSegmentHumanQuery) {
                        EA_datalayer.push('isearchengine');
                        EA_datalayer.push(searchSegmentHumanQuery.innerText);
                        fromSearch = true;
                    } else {
                        {% if post_vars.user_query %}
                            EA_datalayer.push('isearchengine');
                            EA_datalayer.push('{{ post_vars.user_query | escape }}');
                            fromSearch = true;
                        {% endif %}
                    }
                }
                if (fromSearch) {
                    let isearchresults = 0;
                    /* ========= Recherche simple ========= */
                    let nbResults = document.querySelector('#searchResult-nbResults');
                    if (nbResults) {
                        isearchresults = nbResults.innerText;
                    } else {
                        /* ========= Recherche dans un segment ========= */
                        let nbResultsSegment = document.querySelector('#segment_search_nb_results');
                        if (nbResultsSegment) {
                            isearchresults = nbResultsSegment.innerText;
                        } else {
                            /* ========= Recherche affiliee ========= */
                            let nbResultsAffiliate = document.querySelector('#searchResult-nbResults-affiliate');
                            if (nbResultsAffiliate) {
                                isearchresults = nbResultsAffiliate.innerText;
                            }
                        }
                    }
                    EA_datalayer.push('isearchresults');
                    EA_datalayer.push(isearchresults);
                }

                /* ========= Envoi Eulerian ========= */
                if (window.EA_push) {
                    window.EA_push(EA_datalayer);
                }

                /* ========= Tracking clics ========= */
                if (typeof analyticsSerciceAddEvents === 'function') {
                    analyticsSerciceAddEvents('EA_push');
                }
            })();
        </script>
		";
	}
	
	public static function get_default_consent_template() {
		return "
		<script>
	        tarteaucitron.services.eulerian = {
    	        'key': 'eulerian',
    	        'type': 'analytic',
    	        'name': 'Eulerian Analytics',
    	        'needConsent': true,
    	        'cookies': ['etuix'],
    	        'uri' : '{{ domain }}',
    	        'js': function () {
    	        'use strict';
    	        (function(x,w){ if (!x._ld){ x._ld = 1;
    	        let ff = function() { if(x._f){x._f('tac',tarteaucitron,1)} };
    	        w.__eaGenericCmpApi = function(f) { x._f = f; ff(); };
    	        w.addEventListener('tac.close_alert', ff);
    	        w.addEventListener('tac.close_panel', ff);
    	        }})(this,window);
    	        },
    	        'fallback': function () { this.js(); }
    	    };
            (tarteaucitron.job = tarteaucitron.job || []).push('eulerian');
        </script>";
	}
}