Elastic Path Commerce Development

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.

The following listeners are supported by Elastic Path Commerce out of the box:

  • CustomerLoginEventListener: these listeners are called when the visitor signs in to their store account.

The listener interface exposes 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();
          userAgentTag.setValue(userAgent);
          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"
	class="com.elasticpath.sfweb.service.impl.WebCustomerSessionServiceImpl">
	<property name="beanFactory">
		<ref bean="coreBeanFactory" />
	</property>
	<property name="customerSessionService">
		<ref bean="customerSessionService" />
	</property>
	<property name="shoppingCartService">
		<ref bean="shoppingCartService" />
	</property>
	<property name="requestHelper">
		<ref bean="requestHelper" />
	</property>
	<property name="storeProductService">
		<ref bean="storeProductService" />
	</property>
	<property name="newHttpSessionEventListeners">
		<list>
			<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"/>
		</list>
	</property>
...