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

For each event that we must capture analytics data for, create a vendor-specific tag class that extends the base tag class. For example, 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 we can use to send additional information to the external analytics system, if necessary.


Before starting

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

Analytics tag library for HCL Commerce


Task info

The vendor-specific tag class must:


Procedure

  1. Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.

  2. Create a package for our 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 to use. The following list shows the tag names mapped to the base tag classes:

    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.


Next topic: Creating a tag library definition file