Creating vendor-specific tag classes by extending the base tag classes

For each event that you must capture analytics data for, create a vendor-specific tag class that extends the base tag class. For example, if you want to capture data about product view events, extend the ProductBaseTag class. Each base tag class generates standard data for its associated event; however, the base tag classes support optional parameters you can use to send additional information to the external analytics system, if necessary.

Before you begin

Review the following topic to familiarize yourself with the available base tags:

Analytics tag library for IBM WebSphere Commerce

About this task

The vendor-specific tag class must:
  • Extend the base tag class for the corresponding event
  • Implement the logic to generate the vendor-specific JavaScript functions
  • Write the generated JavaScript to the HTML output stream

Procedure

  1. Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.
  2. Create a package for your vendor-specific tag classes:
    1. Navigate to WebSphereCommerceServerExtensionsLogic > src.
    2. Right-click the src folder; then click New > Package.
    3. In the Name field, type com.your_company_name.bi.taglib
    4. Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field.
    5. Click Finish.
  3. In the new package, create a new vendor-specific tag implementation class that extends from each base tag class that you want to use. The following list shows the tag names mapped to the base tag classes:
    • Page view tag – com.ibm.commerce.bi.taglib.CommonBaseTag
    • Product tag – com.ibm.commerce.bi.taglib.ProductBaseTag
    • Shopping cart tag – com.ibm.commerce.bi.taglib.CartBaseTag
    • Order tag – com.ibm.commerce.bi.taglib.OrderBaseTag
    • Registration tag – com.ibm.commerce.bi.taglib.MembershipBaseTag
    • Campaign URL tag – com.ibm.commerce.bi.taglib.CampaignBaseTag
    • Content URL tag – com.ibm.commerce.bi.taglib.BaseTag

    Here is an example of a vendor-specific tag class that extends the CommonBaseTag class for page views:

    public class MyPageViewTag extends CommonBaseTag {
    
    } 
    
  4. In each vendor-specific tag class, implement the logic to send the analytics data to the external analytics system.

    Here is sample code for a vendor-specific implementation of the page view tag. The external analytics vendor uses a JavaScript tagging function to capture the analytics data. For this vendor, the tag implementation class must write the JavaScript to the output stream. The vendor's JavaScript function for page view requires only the page name as a parameter:

    public int doEndTag() throws JspTagException {
       final String METHODNAME = "doEndTag";
       final String PAGEVIEW_TAG = "myPageViewTag";
    
       if (getConfig().isEnabled(getCommandContext().getStoreId())) {
          try {
             HashMap paraMap = getParamMap();
             if (paraMap != null) {
                StringBuffer tags = new StringBuffer();
                ArrayList paramList = new ArrayList();
                String pageName = (String) paraMap
                      .get(TagConstants.PAGE_ID_KEY);
                String storeId = (String) paraMap
                      .get(TagConstants.STORE_ID_KEY);
                Integer strId = Integer.parseInt(storeId);
                if (pageName == null || pageName.trim().length() == 0) {
                   pageName = "\"'\" + document.title + \"'\"";
                } else {
                   pageName ="\"" + UIUtil.toJavaScript(pageName)+ "\"";
                }
                tags.append(PAGEVIEW_TAG);
                tags.append("(");
                tags.append(pageName);
                tags.append(");");
                
                // Get the analytics configuration registry instance to write any
                // vendor specific configuration part along with the tagging function.
                // The configuration will be defined in the biConfig.xml file
                StringBuffer out = new StringBuffer(getConfig()
                    .getInstrumentation(strId));
                out.append(getConfig().getHeader(strId));
                out.append(tags.toString());
                out.append(getConfig().getFooter(strId));
    
     
                // Write the generated JavaScript tagging function to the
                // output stream
                pageContext.getOut().write(out.toString());
             }
          } catch (Exception e) {
             if (ECTrace.traceEnabled(ECTraceIdentifiers.COMPONENT_BI)) {
                ECTrace.trace(ECTraceIdentifiers.COMPONENT_BI, CLASSNAME,
                    METHODNAME, " Exception caught :" + e.getMessage());
                throw new JspTagException(e.getMessage());
             }
          }
       }
       return EVAL_PAGE;
    }
     
  5. Save the class file.