User Segments Front-End Guide

The User Segmentation feature allows you to create custom lists of users. These segments are based on the criteria specified by you - and dynamically update inline with new customer data and changing habits. Segments can then be used to identify customers, allowing you to display bespoke content, or target them with tailored promotions based on their habits.

This article shows you how they can be implemented in your stores templates.

Smarty Functions

❗️

Content generated by Smarty tags, (including that based on user segmentation rules), will be cached if a view cache is used (i.e. Varnish).

For example, with Varnish enabled, if smarty tags are used to present user specific content, the first visit to a page, for the first user, will be cached - and therefore display the same content to all subsequent users. To prevent this, use the Ajax functions (see below), to alter page content as needed.

Recommendation Data

The recommendation data comes from the Smarty tag "user_segment". You must first retrieve the list into a Smarty variable, which you save as an array of users. This array can then be used in a loop, like the example below:

{user_segment segment_name="Some meaningful User Segment Name" users="users" }

{foreach from = $users item=user}
User Name: {$user.firstname} <br/>
{/foreach}

Smarty Parameters

ParameterExampleMandatoryExplanation
limit20NThe number of users that will be shown for the list.
offset20NInitial position of the list.

20 means "take users after the 20th user in the list"
user_cookie76b5ba2bd2ed235fb8c8NWhich user should be returned by the list, by its user_cookie
segment_nameBest BuyersYName of the segment used.
minimum4NMinimum number of users to display. If less than this number of users are available in the segment, then the component is not displayed.
template"user/user_list_template.tpl.html"NUse a template file saved into front-end templates folder. The output parameter "users" will set an array that will contain all the users for this User Segment List and it can be used inside the template.
usersit is an output parameterYArray output name, retrieved by the user segments function.

The array elements will be:

user_cookie
email_address
title
firstname
lastname
company
* dob

Check if a User belongs to a Segment

This Smarty tag checks if the current user belongs to a particular segment. You can include any customisation between IF / ELSE / END as the example below:

{if_user_belongs_segment segment_name="Some meaningful User Segment Name"}
yes - then print this content
{if_user_belongs_segment_else}
No - then print this content instead
{/if_user_belongs_segment}

Promotions and User Segments

It is possible to create promotions that can only be visible for users belonging to a particular segment. For this scenario, it's also possible to use the Smarty Tag if_user_segment_promotion to show specific content for these cases:

{if_user_segment_promotion promotion_name="Saturday Delivery"}

PROMOTION VALID

{if_user_segment_promotion_else}

PROMOTION INVALID

{/if_user_segment_promotion}

If the promotion is valid, you can also include an extra Smarty tag to generate a unique coupon code (one per User/Promotion).

For this, use the Smarty Tag new_promotion_coupon below:

{if_user_segment_promotion promotion_name="Saturday Delivery"}

PROMOTION VALID
New Coupon Generated: {new_promotion_coupon promotion_name="Saturday Delivery"}

{if_user_segment_promotion_else}

PROMOTION INVALID
None coupon generated

{/if_user_segment_promotion}

Ajax Functions

Parameters

The same parameters (apart from template) can be used with Ajax functions.

Recommendation Data

Similar to the same Smarty tag function above, it will return a list of users that belong to a particular segment. See the example below:

$.ajax({url: '/ajax/Ajax_User_Segments/getUserSegment',
data: {   
    "segment_name": 'Some meaningful User Segment Name'
},
dataType: 'json',
success:function(data) {
    console.log(data);
}
});

Check if User belongs to a Segment

Similar to the same Smarty tag function above, it will return true if the current user belongs to a particular segment. See the example below:

$.ajax({url: '/ajax/Ajax_User_Segments/checkUserIsSegmentValid',
data: {   
    "segment_name": 'Auroracommerce fine - Order Total Less than 20'
},
dataType: 'json',
success:function(data) {
    console.log(data);
}
});