request.setAttribute("CLICKSTREAM_RECORDED", true);
String pointer = request.getRequestURI();
HttpSession session = request.getSession();
Clickstream clickstream = (Clickstream) request.getSession().getAttribute("clickstream");
if (clickstream == null) {
clickstream = new Clickstream();
session.setAttribute("clickstream", clickstream);
}
String associatedIdentifier = request.getParameter("id");
if (!UtilMethods.isSet(associatedIdentifier)) {
associatedIdentifier = (String) request.getAttribute(WebKeys.CLICKSTREAM_IDENTIFIER_OVERRIDE);
}
if (!UtilMethods.isSet(associatedIdentifier)) {
associatedIdentifier = APILocator.getIdentifierAPI().find(host, pointer ).getInode();
}
if (UtilMethods.isSet(associatedIdentifier)) {
clickstream.setLastPageId(associatedIdentifier);
clickstream.setLastRequest(new Date());
}
if (clickstream.getHostname() == null) {
clickstream.setHostname(request.getRemoteHost());
}
if (clickstream.getRemoteAddress() == null) {
clickstream.setRemoteAddress(request.getRemoteAddr());
}
// if this is the first request in the click stream
if (clickstream.getClickstreamRequests().size() == Config.getIntProperty("MIN_CLICKSTREAM_REQUESTS_TO_SAVE", 2)) {
// setup initial referrer
if (request.getHeader("Referer") != null) {
clickstream.setInitialReferrer(request.getHeader("Referer"));
} else {
clickstream.setInitialReferrer("");
}
if (request.getHeader("User-Agent") != null) {
clickstream.setUserAgent(request.getHeader("User-Agent"));
} else {
clickstream.setUserAgent("");
}
BrowserSniffer bs = new BrowserSniffer(request.getHeader("User-Agent"));
session.setAttribute("browserSniffer", bs);
clickstream.setBrowserName(bs.getBrowserName());
clickstream.setOperatingSystem(bs.getOS());
clickstream.setBrowserVersion(bs.getBrowserVersion());
clickstream.setMobileDevice(bs.isMobile());
clickstream.setBot(BotChecker.isBot(request));
clickstream.setFirstPageId(associatedIdentifier);
clickstream.setHostId(host.getIdentifier());
}
// Set the cookie id to the long lived cookie
if (!UtilMethods.isSet(clickstream.getCookieId())) {
String _dotCMSID = "";
if(!UtilMethods.isSet(UtilMethods.getCookieValue(request.getCookies(),
com.dotmarketing.util.WebKeys.LONG_LIVED_DOTCMS_ID_COOKIE))) {
Cookie idCookie = CookieUtil.createCookie();
}
_dotCMSID = UtilMethods.getCookieValue(request.getCookies(),
com.dotmarketing.util.WebKeys.LONG_LIVED_DOTCMS_ID_COOKIE);
clickstream.setCookieId(_dotCMSID);
}
// set the user if we have it
if (session.getAttribute(WebKeys.CMS_USER) != null && clickstream.getUserId() == null) {
User user = (User) session.getAttribute(WebKeys.CMS_USER);
setClickStreamUser(user.getUserId(), request);
}
ClickstreamRequest cr = ClickstreamRequestFactory.getClickstreamRequest(request, clickstream.getLastRequest());
clickstream.setNumberOfRequests(clickstream.getNumberOfRequests() + 1);
cr.setRequestOrder(clickstream.getNumberOfRequests());
cr.setHostId(host.getIdentifier());
cr.setAssociatedIdentifier(associatedIdentifier);
// prevent dupe entries into the clickstream table - just retun if the user is on the same page
if(clickstream.getClickstreamRequests() != null &&clickstream.getClickstreamRequests().size()>0){
ClickstreamRequest last = clickstream.getClickstreamRequests().get(clickstream.getClickstreamRequests().size()-1);
if(last != null && cr.getAssociatedIdentifier().equals( last.getAssociatedIdentifier())){
return clickstream;
}
}
clickstream.addClickstreamRequest(cr);
return clickstream;
}