The client object and methods for client-scoped personalization data.
{{ client }}
The client object is available on every page, and contains information about the history of the browser used to access the site. Note that most of these properties are only meaningful if the user has allowed permission for sessions. Additionally, client properties require cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) or without permission for session will always behave like an initial page-load without existing client information. The client object is one of a handful of simple mechanisms to enable personalization on your site. Used well, these can be powerful tools for developers and website owners.
The date that we received the first request from this client. Note that this is dependent on a number of outside factors, including the client's use (and/or clearing) of cookies and their allowance of sessions
The full list of custom properties that are set for the current client. Note that this list only includes the keys, the values will have to be retrieved using the keys
The maximum number of properties that may be stored on the client object. Adding new properties beyond that limit will result in the oldest properties being replaced by the newer properties. The default limit to the number of properties is currently 100. If you need more than 100 client properties contact Marketpath about raising your limit
JSON representation of the client object, similar to {{ client | inspect: 4, false }}
The client object is copyable, and when copied using the {% copy_to_dictionary %} method, the keys will be the custom property names and the values will be the corresponding custom property values. The client object may also be enumerated as a list of strings, which will be the names of the custom properties saved on the client. Note that this list will only include the property names when the enumeration started and adding or removing client properties during enumeration will not affect the property names for the current enumeration.
ExampleCheck if this is the client's first sessionDetect the first time a client has a session (e.g. for onboarding or messaging).
Liquid
{%- if client.allowed and client.num_sessions == 1 -%}
<p>This is your first session {% if session.first_page %}AND your first page!{% endif %}</p>
{%- endif -%}
ExampleHow to use the set_client method to store custom data on the client
This example uses the set_client method to save multiple dynamic values on the client for later use. The submittedForms variable is incremented by 1, the lastKnownEmail variable is set to the submission email, and a dynamic variable constructed from the name of the form prepended by 'submitted' will also be set to 'true'.
ExampleCount client sessions within a time window (e.g. one year)Count how many sessions the client has had within a time window (e.g. one year) for analytics or limits.
Liquid
{%- if client.allowed and client.num_sessions > 6 and client.VIP != "true" and client.first_request_date < request.date | add_years: -1 -%}
<p>You have viewed this site {{ client.num_sessions }} since {{ client.first_request_date | date: 'MMMM yyyy' }}. During that time you have looked at {{ client.num_pages }} pages. We are happy that you have taken so much interest in us, and would like to invite you to <a href="#"> join our VIP club</a>!</p>
{%- endif -%}
ExampleList all custom properties on client, session, or userIterate over the client, session, or profile to list all custom properties. The same patterns works for each object type.
Enumerate Client properties
Liquid
<h4>Client Properties:</h4>
<ul>
{%- for property in client -%}
<li><strong>{{property}}</strong> = {{ client[property] }}</li>
{%- endfor -%}
</ul>
This example outputs a list of all of the client properties for the current client. Using {% for property in client.properties %} would produce the same result.
Enumerate Session properties
Liquid
<h4>Session Properties:</h4>
<ul>
{%- for property in session -%}
<li><strong>{{property}}</strong> = {{ session[property] }}</li>
{%- endfor -%}
</ul>
This example outputs a list of all of the session properties for the current session. Using {% for property in session.properties %} would produce the same result.
Enumerate Profile attributes (mostly safe)
Liquid
<h4>Profile Attributes:</h4>
<ul>
{%- for property in profile -%}
<li><strong>{{property}}</strong> = {{ profile[property] }}</li>
{%- endfor -%}
</ul>
This example will work as long as there are no profile settings with the same names as the profile attributes. In that case, the profile setting would be output instead of the profile attribute. To avoid this, you can use profile.attributes[property] instead.
Enumerate Profile attributes (safe)
Liquid
<h4>Profile Attributes:</h4>
<ul>
{%- for property in profile -%}
<li><strong>{{property}}</strong> = {{ profile.attributes[property] }}</li>
{%- endfor -%}
</ul>
Outputs a list of all of the profile settings for the current profile, and does not risk outputting profile settings instead of profile attributes.
ExampleWorking with specific object propertiesThere are multiple ways to reference properties on most objects. This example demonstrates several patterns for accessing properties on a few different object types.
Reference properties on the user object
Liquid
{%- if user.is_valid -%}
{%- if user.custom_property_name -%}
<p>Custom Property: {{user.custom_property_name}}</p>
{%- endif -%}
{%- if user['custom_property_name_2'] -%}
<p>Custom Property 2: {{user['custom_property_name_2']}}</p>
{%- endif -%}
{%- endif -%}
Reference properties on the client object
Liquid
{%- if client.is_valid -%}
{%- if client.custom_property_name -%}
<p>Custom Property: {{client.custom_property_name}}</p>
{%- endif -%}
{%- if client['custom_property_name_2'] -%}
<p>Custom Property 2: {{client['custom_property_name_2']}}</p>
{%- endif -%}
{%- endif -%}
Reference properties on the session object
Liquid
{%- if session.is_valid -%}
{%- if session.custom_property_name -%}
<p>Custom Property: {{session.custom_property_name}}</p>
{%- endif -%}
{%- if session['custom_property_name_2'] -%}
<p>Custom Property 2: {{session['custom_property_name_2']}}</p>
{%- endif -%}
{%- endif -%}
Saves custom properties on the client that will survive across multiple sessions until they are changed, unset, or the "session" permission expires. Note that this doesn't mean much unless the user (or the developer) has granted permission for sessions.
{% set_client properties %}Parameters
propertiesrequireddictionary
Key:value pairs with unique keys. May use the variable arguments syntax.
ExampleHow to use the set_client method to store custom data on the client
This example uses the set_client method to save multiple dynamic values on the client for later use. The submittedForms variable is incremented by 1, the lastKnownEmail variable is set to the submission email, and a dynamic variable constructed from the name of the form prepended by 'submitted' will also be set to 'true'.
{% unset_client %}
Removes custom properties from the client.
{% unset_client properties? %}Parameters
propertiesoptionallist
One or more values. May use the variable arguments syntax. The names of the properties to remove. If not included, all properties will be removed
ExampleUnset the client when Do Not Track is presentWhen the Do Not Track header is present, unset the client so no client-specific data is stored.
Liquid
{%- if client_permissions.do_not_track -%}
{%- unset_client -%}
{%- endif -%}
ExampleHow to use the unset_client method to clear client properties
Unsets the specified client properties if the "reset_counters" query parameter is present.
Unset Client Properties Dynamically
Liquid
{%- var props = request.query_params.clearprops | split: ',' -%}
{%- for prop in props -%}
{%- unset_client &prop -%}
{%- endfor -%}
Checks if the "clearprops" query parameter was included in the request, and if it was, unset the specified client properties using the reference variable syntax.
Checks if the "clearprops" query parameter was included in the request, and if it was, converts the comma-delimited list of property names to a space-delimited list and unsets the client properties with those names using the expanded variable syntax.