ampproject / ampproject/amphtml

Add support for client-side sessionization

Open
#1,612 25 comments 0 reactions 1 assignee Claimed by @cramforce View on GitHub
Component: amp-analytics Stale Type: Feature Request WG: analytics
Dominant language
JavaScript
Stars
14.9k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

Analytics vendors like Parse.ly benefit from client-side sessionization in their standard JavaScript tracker and would like to see this supported, if possible, in AMP.

Sessions are a grouping of time-ordered pixel events such that the time between any two events does not exceed some threshold (usually 30m).

As far as the Parse.ly implementation goes, the following session parameters are tracked and sent along with pixels:

| Name | Description | Example |
| --- | --- | --- |
| Session ID | Unique identifier for the session. An auto incrementing integer (0-indexed) that also tracks the total count of sessions. | `2` |
| Initial Session URL | Initial URL of the session (the URL of the first pixel request) | `http://example.com/` |
| Session Referrer | `document.referrer` of the initial URL in the session | `https://www.google.com/` |
| Session Timestamp | Time in milliseconds since the Unix epoch that the session started | `1453905846108` |
| Prior Session Timestamp | Time in milliseconds of the session prior to this one (or 0 if this is the first session for a user) | `1453842891129` |

Session information can be stored either using cookies or some other means like local storage.

The following pseudocode gives an example implementation of fetching client-side session parameters:

``` javascript
// Assume StorageEngine pulls relevant info from cookies/local storage

/*
Assume StorageEngine.get('visitor') returns an object from long-lived storage (i.e. expires after 1 or more years of inactivity)
{
visitorId: ...,
sessionCount: 2, // == session ID
lastSessionTimestamp: 1453842891129
};

Likewise, assume StorageEngine.get('session') returns an object from short-lived storage (i.e. expires after 30m of inactivity)
{
id: 2, // == sessionCount
url: ...,
referrer: ...,
timestamp: ...,
}
*/
function getSession() {
var visitor = StorageEngine.get('visitor');
var session = StorageEngine.get('session');
if (typeof session == 'undefined') {
// New session, increment the session count
visitorInfo.sessionCount++;

session = {
id: visitor.sessionCount,
url: document.location.href,
referrer: document.referrer,
timestamp: (new Date()).getTime(),
lastSessionTimestamp: visitor.lastSessionTimestamp
};
StorageEngine.set('session', session);

visitorInfo.lastSessionTimestamp = session.timestamp;
StorageEngine.set('visitor', visitor);
} else {
// Session already exists, but since we have activity, expiry should be extended by 30m
StorageEngine.extendExpiry('session');
}

// Session variables can now be used in amp-analytics pixels
return session;
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.