Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Creating tag event listeners

Creating tag event listeners

The Tagging Framework uses an event listener model to populate tag values. Listeners are invoked by the application when specific events occur, for example, when the shopper logs on to their store account. To populate a tag, you create a listener and add it to the appropriate collection.

Two types of listeners are supported by Elastic Path Commerce out of the box:

  • NewHttpSessionEventListener: these listeners are called when the visitor arrives at the storefront and an HTTP session is created.
  • CustomerLoginEventListener: these listeners are called when the visitor signs in to their store account.

Both listener interfaces expose an execute method that takes a CustomerSession and an HttpServletRequest as parameters.

To add a new tag, you first need to determine how the value of the tag will be set. If the value of the tag can be retrieved from the HTTP request, your listener class should implement NewHttpSessionEventListener. If the value of the tag is only available after the customer has logged on to their Elastic Path account, then create a listener class that implements CustomerLoginEventListener. The class must implement the execute method to set the tag value and add it to the tag set. The following is an example of a tag event listener implementation. Every time a new session is created, this listener's execute method is called to assign a value to a tag named USER_AGENT. The tag is then added to the shopper's tag set.

package com.elasticpath.sfweb.listeners;

import javax.servlet.http.HttpServletRequest;
import com.elasticpath.commons.listeners.NewHttpSessionEventListener;
import com.elasticpath.domain.customer.CustomerSession;
import com.elasticpath.tags.Tag;
import com.elasticpath.tags.TagSet;

public class UserAgentTagger implements NewHttpSessionEventListener {

     private static final String USER_AGENT = "USER_AGENT";

     public void execute(CustomerSession session, HttpServletRequest request) {
          // get the user agent header string
          String userAgent = request.getHeader("User-Agent");

          // get the shopper's tag set
          TagSet tagSet = session.getCustomerTagSet();

          // set the user agent in the shopper's tag set
          Tag userAgentTag = new Tag();
          tagSet.addTag(USER_AGENT, userAgentTag);

The last step is to make sure the listener is invoked when a new customer session is created. The webCustomerSessionService has a newHttpSessionEventListeners collection. This collection contains the listeners that will be invoked when a session is created. In the storefront web application's serviceSF.xml file (located in WEB-INF/conf/spring/service), add a bean definition for the listener.

<bean id="userAgentTagger" class="com.elasticpath.sfweb.listeners.UserAgentTagger"/>

Then add a reference to the bean listener in the newHttpSessionEventListeners collection.

<bean id="webCustomerSessionService"
	<property name="beanFactory">
		<ref bean="coreBeanFactory" />
	<property name="customerSessionService">
		<ref bean="customerSessionService" />
	<property name="shoppingCartService">
		<ref bean="shoppingCartService" />
	<property name="requestHelper">
		<ref bean="requestHelper" />
	<property name="storeProductService">
		<ref bean="storeProductService" />
	<property name="newHttpSessionEventListeners">
			<ref bean="startShoppingTimeTagger"/>
			<ref bean="customerProfileTagger"/>
			<ref bean="targetUrlTagger"/>
			<ref bean="referringUrlTagger"/>
			<ref bean="referralSearchEngineQueryTagger"/>
			<ref bean="sellingChannelTagger"/>
			<ref bean="geoIpTagger"/>

			<!-- Reference to new listener: -->
			<ref bean="userAgentTagger"/>