Skip to content

Commit

Permalink
#657 re enable goog analytics (#667)
Browse files Browse the repository at this point in the history
* initial work for goog analytics #657

* expose clientId (hashed session id if present) for goog analytics #657

* refactor: move anaylitcsconf in a separate file #657

* analytics conf: create factory method, expose in global alf.io info object too #657
  • Loading branch information
syjer authored and cbellone committed Jun 19, 2019
1 parent 205d574 commit 2cc2b11
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 21 deletions.
16 changes: 14 additions & 2 deletions src/main/java/alfio/controller/api/v2/InfoApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@

import alfio.config.Initializer;
import alfio.controller.api.v2.model.AlfioInfo;
import alfio.controller.api.v2.model.AnalyticsConfiguration;
import alfio.manager.system.ConfigurationManager;
import alfio.model.system.ConfigurationKeys;
import lombok.AllArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Set;

@RestController
@RequestMapping("/api/v2/")
@AllArgsConstructor
public class InfoApiController {

private final Environment environment;
private final ConfigurationManager configurationManager;

@GetMapping("info")
public AlfioInfo getInfo() {
public AlfioInfo getInfo(HttpSession session) {

var demoMode = environment.acceptsProfiles(Profiles.of(Initializer.PROFILE_DEMO));
var devMode = environment.acceptsProfiles(Profiles.of(Initializer.PROFILE_DEV));
var prodMode = environment.acceptsProfiles(Profiles.of(Initializer.PROFILE_LIVE));

return new AlfioInfo(demoMode, devMode, prodMode);

var conf = configurationManager.getFor(Set.of(ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE, ConfigurationKeys.GOOGLE_ANALYTICS_KEY));

var analyticsConf = AnalyticsConfiguration.build(conf, session);

return new AlfioInfo(demoMode, devMode, prodMode, analyticsConf);
}
}
1 change: 1 addition & 0 deletions src/main/java/alfio/controller/api/v2/model/AlfioInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public class AlfioInfo {
private final boolean demoModeEnabled;
private final boolean devModeEnabled;
private final boolean prodModeEnabled;
private final AnalyticsConfiguration analyticsConfiguration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.api.v2.model;

import alfio.manager.system.ConfigurationManager;
import alfio.model.system.ConfigurationKeys;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Map;

import static alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE;
import static alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY;

@AllArgsConstructor
@Getter
public class AnalyticsConfiguration {
private final String googleAnalyticsKey;
private final boolean googleAnalyticsScrambledInfo; //<- see GOOGLE_ANALYTICS_ANONYMOUS_MODE
private final String clientId;


public static AnalyticsConfiguration build(Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> conf, HttpSession session) {
var googAnalyticsKey = StringUtils.trimToNull(conf.get(GOOGLE_ANALYTICS_KEY).getValueOrDefault(null));
var googAnalyticsScrambled = conf.get(GOOGLE_ANALYTICS_ANONYMOUS_MODE).getValueAsBooleanOrDefault(true);

var sessionId = session.getId();
var clientId = googAnalyticsKey != null && googAnalyticsScrambled && sessionId != null ? DigestUtils.sha512Hex(sessionId) : null;
return new AnalyticsConfiguration(googAnalyticsKey, googAnalyticsScrambled, clientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class EventWithAdditionalInfo implements DateValidity {
private final PromotionsConfiguration promotionsConfiguration;
//

private final AnalyticsConfiguration analyticsConfiguration;

public String getShortName() {
return event.getShortName();
}
Expand Down Expand Up @@ -186,6 +188,10 @@ public PromotionsConfiguration getPromotionsConfiguration() {
return promotionsConfiguration;
}

public AnalyticsConfiguration getAnalyticsConfiguration() {
return analyticsConfiguration;
}

@AllArgsConstructor
@Getter
public static class PaymentProxyWithParameters {
Expand Down Expand Up @@ -227,4 +233,5 @@ public static class PromotionsConfiguration {
private final boolean hasAccessPromotions;
private final boolean usePartnerCode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -113,7 +114,7 @@ public ResponseEntity<List<BasicEventInfo>> listEvents() {
}

@GetMapping("event/{eventName}")
public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName") String eventName) {
public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName") String eventName, HttpSession session) {
return eventRepository.findOptionalByShortName(eventName).filter(e -> e.getStatus() != Event.Status.DISABLED)//
.map(event -> {

Expand All @@ -138,7 +139,9 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
ENABLE_ATTENDEE_AUTOCOMPLETE,
ENABLE_TICKET_TRANSFER,
DISPLAY_DISCOUNT_CODE_BOX,
USE_PARTNER_CODE_INSTEAD_OF_PROMOTIONAL
USE_PARTNER_CODE_INSTEAD_OF_PROMOTIONAL,
GOOGLE_ANALYTICS_KEY,
GOOGLE_ANALYTICS_ANONYMOUS_MODE
));

var geoInfoConfiguration = Map.of(
Expand Down Expand Up @@ -177,9 +180,6 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
var formattedEndDate = Formatters.getFormattedDate(event, event.getEnd(), "common.event.date-format", messageSource);
var formattedEndTime = Formatters.getFormattedDate(event, event.getEnd(), "common.event.time-format", messageSource);


var partialConfig = Configuration.from(event);

//invoicing information
boolean canGenerateReceiptOrInvoiceToCustomer = configurationManager.canGenerateReceiptOrInvoiceToCustomer(event);
boolean euVatCheckingEnabled = vatChecker.isReverseChargeEnabledFor(event.getOrganizationId());
Expand Down Expand Up @@ -210,10 +210,15 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
var promoConf = new EventWithAdditionalInfo.PromotionsConfiguration(hasAccessPromotions, usePartnerCode);
//

//analytics configuration
var analyticsConf = AnalyticsConfiguration.build(configurationsValues, session);
//

return new ResponseEntity<>(new EventWithAdditionalInfo(event, ld.getMapUrl(), organization, descriptions, availablePaymentMethods,
bankAccount, bankAccountOwner,
formattedBeginDate, formattedBeginTime,
formattedEndDate, formattedEndTime, invoicingConf, captchaConf, assignmentConf, promoConf), getCorsHeaders(), HttpStatus.OK);
formattedEndDate, formattedEndTime,
invoicingConf, captchaConf, assignmentConf, promoConf, analyticsConf), getCorsHeaders(), HttpStatus.OK);
})
.orElseGet(() -> ResponseEntity.notFound().headers(getCorsHeaders()).build());
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/alfio/util/TemplateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@


public enum TemplateResource {
GOOGLE_ANALYTICS("/alfio/templates/google-analytics.ms", false, "text/plain", TemplateManager.TemplateOutput.TEXT),

@Deprecated
GOOGLE_ANALYTICS("", false, "", TemplateManager.TemplateOutput.TEXT),

CONFIRMATION_EMAIL_FOR_ORGANIZER("/alfio/templates/confirmation-email-for-organizer-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
Expand Down
9 changes: 0 additions & 9 deletions src/main/resources/alfio/templates/google-analytics.ms

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,13 @@ public void reservationFlowTest() throws Exception {
assertEquals("o", translationsApiController.getPublicTranslations("it").get("common.or"));
assertEquals("oder", translationsApiController.getPublicTranslations("de").get("common.or"));

var alfioInfo = infoApiController.getInfo();
var alfioInfo = infoApiController.getInfo(new MockHttpSession());
assertEquals(false, alfioInfo.isDemoModeEnabled());
assertEquals(true, alfioInfo.isDevModeEnabled());
assertEquals(false, alfioInfo.isProdModeEnabled());
assertEquals(true, alfioInfo.getAnalyticsConfiguration().isGoogleAnalyticsScrambledInfo());
assertEquals(null, alfioInfo.getAnalyticsConfiguration().getGoogleAnalyticsKey());
assertEquals(null, alfioInfo.getAnalyticsConfiguration().getClientId());

//

Expand Down Expand Up @@ -321,10 +324,10 @@ public void reservationFlowTest() throws Exception {
assertEquals(event.getShortName(), events.get(0).getShortName());

//
assertEquals(HttpStatus.NOT_FOUND, eventApiV2Controller.getEvent("NOT_EXISTS").getStatusCode());
assertEquals(HttpStatus.NOT_FOUND, eventApiV2Controller.getEvent("NOT_EXISTS", new MockHttpSession()).getStatusCode());
//

var eventRes = eventApiV2Controller.getEvent(event.getShortName());
var eventRes = eventApiV2Controller.getEvent(event.getShortName(), new MockHttpSession());
assertEquals(HttpStatus.OK, eventRes.getStatusCode());
var selectedEvent = eventRes.getBody();
assertEquals("CHF", selectedEvent.getCurrency());
Expand Down

0 comments on commit 2cc2b11

Please sign in to comment.