Session

Session

{{ session }}

The session object is available on every page, and contains information about the user's current session. Note that most of these properties are only meaningful if the user has allowed permission for sessions. Additionally, sessions require cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) or without permission for sessions will always behave like an initial page-load without existing session information. The session 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.

Properties of {{ session }} objects
Name Type Description
is_valid Boolean Whether or not the user (or template developer) has granted permission for a session
allowed Boolean Whether or not the user (or template developer) has granted permission for a session
id String The unique identifier for the user's session. This will contain a value even if the user is not allowed to have a session, but the value will change on every page load
first_page Boolean True if this is the first page load in the user's current session. Note that this will always return true if the user does not have permission for a session since we will have no "memory" of prior page loads for the current session
start_date time The time that the current session started (equivalent to the time that the first request was made for this session)
end_date time The time that the current session will expire if no more requests are made. Note that sessions are extended by every request and so the end_date will also be different on every request
num_requests Integer The total number of requests for the current session, including requests resulting in errors
num_pages Integer The total number of successful page requests for the current session. This is different than num_requests in that it does not include any requests which resulted in an HTTP response code other than 200. This also assumes that the current request will return a 200 response code
unique_pages Integer The total number of unique pages requested in the current session. This number could be significantly lower than num_pages if the user requests several pages multiple times
num_errors Integer The total number of errors returned in the current session. This includes any request that returns something other than a 200 response code
properties list The full list of custom properties that have been set for the current session. Note that this list only includes the keys, the values will have to be retrieved using the keys
history list A list containing the last 100 page views as page_view objects for the current session
* String Individual custom properties for the session may be accessed using {{ session.propertyName }} or {{ session['property-name'] }} syntax
output String JSON representation of the session object, similar to calling {{ session | inspect: 3, false }}

The session object is copyable, and when copied using the {% copy_to_dictionary %} method the keys will be the custom session property names and the values will be the corresponding custom property values. You may also treat this object as a list containing all of the property names which may be iterated using a {% for %} loop.

Related

Root Scope Reserved Variables

Every page on the site is created by processing templates using specific pre-defined inputs. These reserved variables are present on the root scope of every pageload.

object

May be any object, including simple, complex and list objects. In some cases may even include symbols and null.

{{ 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.

{{ code }}

{{ page_view }}

The page_view object contains information about a single HTTP request and response in the current session.

{{ date }}

A field containing a user-selected date

{{ request }}

The request object is available on every page, and contains information regarding the HTTP request that may be useful for serving and rendering the page.

{{ permission }}

An object containing information about permission allowed or denied in the permissions system and accessed using the {{ client_permissions }} object.

string

Can be any text, from the empty string ("") to the full HTML output of the template. When used alone in a conditional, all strings evaluates as true - even if they are empty or a value such as "0" or "false".

{{ template }}

number

Can be any number, including integers, decimals, or the value 0. Any value - including 0 - evaluates as true when used alone in a conditional.

{{ page_view }}

An object containing information about a previous request in the current session

{{ time }}

Represents a specific instant in a specific timezone.

integer

A whole (non-fractional, non-decimal) number. May be 0. Any value - including 0 - evaluates as true when used alone in a conditional.

list

An enumerable list containing zero or more objects. Lists may contain many different object types, although most lists only contain a single object type. There are a large number of other complex object types that are also lists (ag: an articlelist is a complex object but is also a list of article objects).

boolean

True or False

{% set_session %}

Saves custom properties on the session. Note that this doesn't mean much unless the user (or the developer) has granted permission for sessions.

{% set_session properties %}

{% set_session
properties
 
Key:value pairs with unique keys. May use the variable arguments syntax.
%}

Related

{% set_title %}

Sets the page title.

{% unset_session %}

Removes custom properties from the session.

{% set_description %}

Sets the meta description for the current page, which is output by default as part of an HTML page's {{ automatic_markup }}.

{% set_timezone %}

Sets the default timezone to use when rendering dates and times on the page that do not already have a separate timezone configured.

{% set_content_type %}

Sets the Content-Type header for the HTTP response.

{% set_robots %}

Sets the robots meta directive.

{% set_client %}

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_favicon %}

Sets the URL to the favicon for the current page, which is output by default as part of an HTML page's {{ automatic_markup }}.

{% set_profile_setting %}

Saves custom values to predefined profile settings that will be accessible whenever the current profile is logged in. Note that this is meaningless unless the user is logged in. Profile settings may include validation, in which case all settings will be validated before being set and any validation error will prevent the setting(s) from being set. Validation errors may optionally be output to a variable.

{% set %}

Replaces a value on the nearest scope where it has already been defined. If it has not been defined yet, it is stored on the root scope.

{% set_header %}

Sets one or more headers in the HTTP response.

{% set_client_permission %}

Defines whether the client has granted or deined permission for a particular feature (eg: sessions). The only permission defined by default is the session permission (configurable in the site properties). However, the template developer may use this mechanism for their own purposes as well. The permissions defined by this method will be stored in the permissions cookie, which may be read and/or modified by client-side javascript.

{% set_dictionary %}

Sets properties on an editable dictionary object. If the dictionary does not exist it will be created and stored on the current scope. If the dictionary exists but is not editable this will throw an error.

{% set_cookie %}

Sets a cookie in the HTTP response.

{% set_canonical_url %}

Sets the canonical URL for the current page, which is output by default as part of an HTML page's {{ automatic_markup }}.

{% set_response_code %}

Sets the HTTP response status code.

{{ session }}

The session object is available on every page, and contains information about the user's current session. Note that most of these properties are only meaningful if the user has allowed permission for sessions. Additionally, sessions require cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) or without permission for sessions will always behave like an initial page-load without existing session information. The session 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.

{% set_profile %}

Saves custom properties on the profile that will be accessible whenever the current profile is logged in. The properties will be saved to the profile's attribute dictionary. Note that this is meaningless unless the user is logged in.

{% unset_session %}

Removes custom properties from the session.

{% unset_session properties? %}

{% unset_session
properties
 
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
%}

Related

{% unset_dictionary %}

Removes properties from an editable dictionary object. If the dictionary does not exist an empty one will be created and stored on the current scope. If the dictionary exists but is not editable this will throw an error.

{% set_session %}

Saves custom properties on the session. Note that this doesn't mean much unless the user (or the developer) has granted permission for sessions.

{% unset_client %}

Removes custom properties from the client.

{% unset_client_permission %}

Removes the specified permissions from the permissions cookie. Note that this is not the same as denying permission since there will be no record that permission was either granted or denied after the permission has been unset.

{% unset_profile %}

Removes custom properties from the attribute dictionary of the currently logged-in profile. Note that this is meaningless unless the user is logged in.

{% unset_profile_setting %}

Removes custom values from the profile settings for the currently logged-in profile. Note that this is meaningless unless the user is logged in. For settings with a default value this will reset them to the default, and for all other settings this will set them to empty/unselected/false. Profile settings may be required and include validation, in which case all settings will be validated before being removed and any validation error will prevent any settings from being set. Validation errors may optionally be output to a variable.

{% unset_cookie %}

"Unsets" one or more cookies. Because of how cookies work, this will actually ADD the cookie to the response with an expiration date in the past.

{{ session }}

The session object is available on every page, and contains information about the user's current session. Note that most of these properties are only meaningful if the user has allowed permission for sessions. Additionally, sessions require cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) or without permission for sessions will always behave like an initial page-load without existing session information. The session 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.

Examples

Unset multiple Session properties

Copy
{% if request.query_params.ad_setting == "false" %}
{% set_client_permission deny ads %}
{% unset_session facebook_advertiser_id google_ads_id other_third_party_ids %}
{% endif %}

Unset Session Properties dynamically

Copy
{% var props = request.query_params.clearprops | split: ',' %}
{% for prop in props %}
{% unset_session &prop %}
{% endfor %}

Set Session advanced

Copy
{% if submission.is_valid %}
{% var forms_submitted = session.submittedForms | to_int | plus: 1 %}
{% var session_formname = form.name.value | prepend:'submitted' %}
{% set_session formSubmitted:"true" submittedForms:forms_submitted domain:submission.domain &session_formname:'true' %}
{% endif %}

Unset Session

Copy
{% if client_permissions.do_not_track %}
{% unset_session %}
{% endif %}

Set Session

Copy
{% if request.query_params.sortby %}
{% set_session sortby:request.query_params.sortby %}
{% endif %}
{% var sortby = session.sortby | default: 'post_date' %}
{% blog_post_collection posts sort_by:sortby %}

List all custom properties for the current session

Copy
<h4>Session Properties:</h4>
<ul>
{% for property in session %} --equivalent to {% for property in session.properties %}
<li><strong>{{property}}</strong> = {{ session[property] }}</li>
{% endfor %}
</ul>

First client Session

Copy
{% 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 %}

Logout clear session

Copy
{% logout clear_session %}

Session first page

Copy
{% if session.first_page %}
<p>This is your first pageload this session!</p>
{% endif %}

Session Errors

Copy
{% if session.num_errors > 3 %}
<p>We appear to be having trouble meeting your needs. Please <a href="#">contact us directly</a> so that we can assist you and fix the trouble for future visitors, or continue browsing for what you need.</p>
{% endif %}

Session Unique Pages

Copy
{% if session.unique_pages > 10 %}
<p>Need help finding what you're looking for? <a href="#">Try this!</a></p>
{% endif %}