I thought this would be useful to all the Sites developers out there.
The <commercecontext.setsegments> tag is useful when under program control you unambiguously *KNOW* the segment(s) of the visitor. With this tag you can just set it as needed without having to do a "calculate segments" based on visitor attributes (which may or may not exist). Additionally, this tag is useful when integrating with other applications that calculate segments external to WebCenter Sites (for example, Oracle Commerce). This tag is also very useful if you want to cache your pagelets against a known segment value. Please note that while it has been tested by engineering, somehow the documentation for this tag missed the boat.
Consult the commercecontext.tld, it already defines this tag. It should "just work" in 11g and all later versions of WebCenter Sites.
<tag> <name>setsegments</name> <tagclass>com.openmarket.gator.jsp.commercecontext.Setsegments</tagclass> <bodycontent>empty</bodycontent> <info> Unconditionally sets the list of segments that the current visitor belongs to. Used for integration with external segmentation engines as well as for debugging. </info> <attribute> <name>names</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
Thus, using the tag <commercecontext:setsegments names='<%=mySegments%>' /> in your JSP template prior to “getting your recommendation” (where String mySegments = a comma delimited list of names of your segments) will do the trick.
It is recommended to implement your visitor segment calculation logic in an uncached outer wrapper or Template then pass the calculated segment down to the appropriate pagelets but not to all pagelets! (for example you probably don’t want to pass segment to your footer which would cause it to overcache).
In uncached outer Template:
For each pagelet/slot on the page:
Be sure to add “segment” as part of the sitecatalog cachecriteria for each Recommendation template.
If you do decide to use the segment value(s) as a cache argument for your slots/pagelets and each visitor can belong to just a single segment, then simply using that value as is will be sufficient. However, if a visitor might belong to more than one segment (e.g. they belong to ThirtySomethings AND they belong to LovesFastCars), then it behooves you to SORT the list of visitor segments using REGEX prior to passing them as a comma-delimited cache argument. This way, all visitors with the same list of shared segments will also share the same cache.
Be aware of possible permutations of cache if too many combinations of segments are allowed.