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.

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.

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

Removes custom properties from the session.

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

Session Properties:

    {% for property in session %} --equivalent to {% for property in session.properties %}
  • {{property}} = {{ session[property] }}
  • {% endfor %}

First client Session

Copy
{% if client.allowed and client.num_sessions == 1 %}

This is your first session {% if session.first_page %}AND your first page!{% endif %}

{% endif %}

Logout clear session

Copy
{% logout clear_session %}

Session first page

Copy
{% if session.first_page %}

This is your first pageload this session!

{% endif %}

Session Errors

Copy
{% if session.num_errors > 3 %}

We appear to be having trouble meeting your needs. Please contact us directly so that we can assist you and fix the trouble for future visitors, or continue browsing for what you need.

{% endif %}

Session Unique Pages

Copy
{% if session.unique_pages > 10 %}

Need help finding what you're looking for? Try this!

{% endif %}