Search in sources :

Example 1 with GOOGLE_ANALYTICS_KEY

use of alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY in project alf.io by alfio-event.

the class DynamicResourcesController method getGoogleAnalyticsScript.

@RequestMapping("/resources/js/google-analytics")
public void getGoogleAnalyticsScript(HttpSession session, HttpServletResponse response, @RequestParam("e") Integer eventId) throws IOException {
    response.setContentType("application/javascript");
    Optional<Event> ev = Optional.ofNullable(eventId).flatMap(id -> Optional.ofNullable(eventRepository.findById(id)));
    ConfigurationPathKey pathKey = ev.map(e -> Configuration.from(e.getOrganizationId(), e.getId(), GOOGLE_ANALYTICS_KEY)).orElseGet(() -> Configuration.getSystemConfiguration(GOOGLE_ANALYTICS_KEY));
    final Optional<String> id = configurationManager.getStringConfigValue(pathKey);
    final String script;
    ConfigurationPathKey anonymousPathKey = ev.map(e -> Configuration.from(e.getOrganizationId(), e.getId(), GOOGLE_ANALYTICS_ANONYMOUS_MODE)).orElseGet(() -> Configuration.getSystemConfiguration(GOOGLE_ANALYTICS_ANONYMOUS_MODE));
    if (id.isPresent() && configurationManager.getBooleanConfigValue(anonymousPathKey, true)) {
        String trackingId = Optional.ofNullable(StringUtils.trimToNull((String) session.getAttribute("GA_TRACKING_ID"))).orElseGet(() -> UUID.randomUUID().toString());
        Map<String, Object> model = new HashMap<>();
        model.put("clientId", trackingId);
        model.put("account", id.get());
        script = templateManager.renderTemplate(TemplateResource.GOOGLE_ANALYTICS, model, Locale.ENGLISH);
    } else {
        script = id.map(x -> String.format(GOOGLE_ANALYTICS_SCRIPT, x)).orElse(EMPTY);
    }
    response.getWriter().write(script);
}
Also used : ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) HttpSession(javax.servlet.http.HttpSession) RequestParam(org.springframework.web.bind.annotation.RequestParam) java.util(java.util) ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) GOOGLE_ANALYTICS_KEY(alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY) TemplateManager(alfio.util.TemplateManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IOException(java.io.IOException) ConfigurationManager(alfio.manager.system.ConfigurationManager) Controller(org.springframework.stereotype.Controller) EventRepository(alfio.repository.EventRepository) StringUtils(org.apache.commons.lang3.StringUtils) GOOGLE_ANALYTICS_ANONYMOUS_MODE(alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE) Configuration(alfio.model.system.Configuration) Event(alfio.model.Event) TemplateResource(alfio.util.TemplateResource) Event(alfio.model.Event) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ConfigurationManager (alfio.manager.system.ConfigurationManager)1 Event (alfio.model.Event)1 Configuration (alfio.model.system.Configuration)1 ConfigurationPathKey (alfio.model.system.Configuration.ConfigurationPathKey)1 GOOGLE_ANALYTICS_ANONYMOUS_MODE (alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE)1 GOOGLE_ANALYTICS_KEY (alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY)1 EventRepository (alfio.repository.EventRepository)1 TemplateManager (alfio.util.TemplateManager)1 TemplateResource (alfio.util.TemplateResource)1 IOException (java.io.IOException)1 java.util (java.util)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Controller (org.springframework.stereotype.Controller)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1