Session and View Tracking

A Session is a timeframe that should indicate the screen time of your user and a View is a way to indicate which page or section of your application your user is seeing on your application.

Web Android iOS Node Flutter React Native Windows Unity Java C++ GTM Dart HarmonyOS API Call

Sessions

Automatic Sessions are when SDK detects the screen time of the user and automatically reports it to your server. Manual Sessions are when you start, update or end a Session as you see fit.

Automatic Sessions

Call after initialization:

Asynchronous Synchronous
Countly.q.push(['track_sessions']);

If Application class is provided during init you can omit these step as SDK would automatically start tracking sessions. If not, after the initialization, you'll need to add the following calls to all your activities.

  • Call Countly.sharedInstance().onStart(this) in onStart, where this is a link to the current Activity.
  • Call Countly.sharedInstance().onStop() in onStop.
  • Call Countly.sharedInstance().onConfigurationChanged(newConfig) in onConfigurationChanged if you want to track the orientation changes.

Automatic Session Tracking is enabled by default.

Not supported.

Automatic Session Tracking is enabled by default.

Automatic Session Tracking is enabled by default.

Not supported.

Automatic Session Tracking is enabled by default.

Not supported.

Automatic Session Tracking is enabled by default.

Automatic sessions can be enabled from initialization template:

Not supported.

Not applicable.

Automatic Session Tracking is enabled by default.

Manual Sessions

Only use the methods below if you aren’t planning on using the automatic session tracking and set the use_session_cookie setting to false during init for more granular control of the session.

SDK will automatically report elapsed session duration with 60 seconds intervals (this can be modified during init with session_update config option).

Asynchronous Synchronous

Countly.q.push(['begin_session']);
Countly.q.push(['end_session']);
// during init
config.enableManualSessionControl();

//after init
Countly.sharedInstance().sessions().beginSession();
Countly.sharedInstance().sessions().updateSession();
Countly.sharedInstance().sessions().endSession();
Objective-C Swift
// during init
config.manualSessionHandling = YES;

// after init
[Countly.sharedInstance beginSession];
[Countly.sharedInstance updateSession];
[Countly.sharedInstance endSession];
Countly.begin_session(true);
Countly.session_duration(seconds);
Countly.end_session(seconds);
// during init
config.enableManualSessionHandling();

// after init
Countly.instance.sessions.beginSession();
Countly.instance.sessions.updateSession();
Countly.instance.sessions.endSession();
const countlyConfig = new CountlyConfig("SERVER_URL", "APP_KEY");
countlyConfig.enableManualSessionControl();
// Optional hybrid mode:
// countlyConfig.enableManualSessionControlHybridMode();

await Countly.initWithConfig(countlyConfig);

await Countly.sessions.beginSession();
await Countly.sessions.updateSession();
await Countly.sessions.endSession();
Countly.Instance.SessionBegin();
Countly.Instance.SessionEnd();
Countly.Instance.SessionUpdate(elapsedTime);

Not supported.

Countly.session().begin();
Countly.session().end();
Countly.session().update()
# during init
cly::Countly::getInstance().enableManualSessionControl();

# later
cly::Countly::getInstance().beginSession();
cly::Countly::getInstance().updateSession();
cly::Countly::getInstance().endSession();

Not supported.

Not supported.

To begin a Session

curl --request POST \
  --url 'https://YOUR_SERVER/i' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data 'begin_session=1' \
  --data 'app_key=APP_KEY' \
  --data 'device_id=DEVICE_ID' \
  --data 'timestamp=TIMESTAMP' \
  --data 'hour=HOUR' \
  --data 'dow=DAYOFTHEWEEK' \
  --data 'tz=TIMEZONE'

To end a Session

curl --request POST \
  --url 'https://YOUR_SERVER/i' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data 'end_session=1' \
  --data 'session_duration=DURATION' \
  --data 'app_key=APP_KEY' \
  --data 'device_id=DEVICE_ID' \
  --data 'timestamp=TIMESTAMP' \
  --data 'hour=HOUR' \
  --data 'dow=DAYOFTHEWEEK' \
  --data 'tz=TIMEZONE'

To inform Session duration (for live data)

curl --request POST \
  --url 'https://YOUR_SERVER/i' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data 'session_duration=DURATION' \
  --data 'app_key=APP_KEY' \
  --data 'device_id=DEVICE_ID' \
  --data 'timestamp=TIMESTAMP' \
  --data 'hour=HOUR' \
  --data 'dow=DAYOFTHEWEEK' \
  --data 'tz=TIMEZONE'
// during init
config.sessions.enableManualControl();

// after init
const sessions = Countly.sharedInstance().sessions;
await sessions.beginSession();
await sessions.updateSession();
await sessions.endSession();

Views

Automatic Views are when SDK detects a page change happens and automatically reports this to your server. Manual Views are when you decide a View starts and when it ends (by starting a new View.)

Automatic Views

Putting this directly after init would record the page SDK was initialized

Asynchronous Synchronous
Countly.q.push(['track_pageview']);
config.enableAutomaticViewTracking();
Objective-C Swift
config.enableAutomaticViewTracking = YES;

Not supported.

Not supported.

const countlyConfig = new CountlyConfig("SERVER_URL", "APP_KEY");
countlyConfig.enableAutomaticViewTracking();
// Optional:
countlyConfig.setAutomaticViewTrackingExclusionList(["ModalView"]);

await Countly.initWithConfig(countlyConfig);

Not supported.

Not supported.

Not supported.

Not supported.

Automatic views can be enabled from initialization template:

Not supported.

config.views.enableAutomaticViewTracking();

Not supported.

Manual Views

Asynchronous Synchronous
Countly.q.push(['track_pageview', "View Name", null, {"custom key":"custom value"}]);
Map<String, Object> segmentation = new ConcurrentHashMap<>();
segmentation.put("custom key", "custom value");
    
Countly.sharedInstance().views().startAutoStoppedView("View Name", segmentation);
Objective-C Swift
[Countly.sharedInstance.views startAutoStoppedView:@"View Name" segmentation:@{@"custom key": @"custom value"}];
Countly.track_view("View Name", {"custom key":"custom value"});
Map<String, Object> segmentation = {"custom key": "custom value"};

final String? anotherViewID = Countly.instance.views.startAutoStoppedView("View Name", segmentation);
const viewSegmentation = {"custom key": "custom value"};

const autoStoppedViewID = await Countly.views.startAutoStoppedView("View Name", viewSegmentation);

const manualViewID = await Countly.views.startView("Another View", viewSegmentation);
await Countly.views.stopViewWithID(manualViewID);
Countly.Instance.RecordView("View Name");
Dictionary<string, object> viewSegmentation = new Dictionary<string, object>();
viewSegmentation.Add("custom key", "custom value");

Countly.Instance.Views.StartAutoStoppedView("View Name", viewSegmentation);
Map<String, Object> viewSegmentation = new HashMap<>();
viewSegmentation.put("custom key", "custom value");

String viewID = Countly.instance().views().startAutoStoppedView("View Name", viewSegmentation);
std::map<std::string, std::string> segmentation = {
  {"custom key", "custom value"} 
};

std::string& viewID = cly::Countly::getInstance().views().openView("View Name", segmentation);

Not supported.

final sdk = Countly.defaultInstance!;

await sdk.views.startAutoStoppedView(
  'View Name',
  segmentation: {'custom key': 'custom value'},
);

await sdk.views.endActiveView();
const views = Countly.sharedInstance().views;
const viewSegmentation: Record<string, string> = { 'custom key': 'custom value' };

// Auto-stopped (ends when another view starts)
await views.startAutoStoppedView('View Name', viewSegmentation);

// Manual view, end explicitly
const id = await views.startView('Another View', viewSegmentation);
await views.stopViewWithID(id!, viewSegmentation);
// or stop by name:
// await views.stopViewWithName('Another View', viewSegmentation);

To start a View:

curl --request POST \
--url "https://YOUR_SERVER/i" \
--header "Accept: application/json" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "app_key=APP_KEY" \
--data "device_id=DEVICE_ID" \
--data "timestamp=TIMESTAMP" \
--data "hour=HOUR" \
--data "dow=DAYOFTHEWEEK" \
--data "tz=TIMEZONE" \
--data "events=[{\"key\":\"[CLY]_view\",\"count\":1,\"segmentation\":{\"name\":\"VIEW_NAME\",\"visit\":1,\"view\":\"END_POINT\",\"domain\":\"DOMAIN\"},\"timestamp\":TIMESTAMP,\"hour\":HOUR,\"dow\":DAYOFTHEWEEK}]"

To end that View:

curl --request POST \
--url "https://YOUR_SERVER/i" \
--header "Accept: application/json" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "app_key=APP_KEY" \
--data "device_id=DEVICE_ID" \
--data "timestamp=TIMESTAMP" \
--data "hour=HOUR" \
--data "dow=DAYOFTHEWEEK" \
--data "tz=TIMEZONE" \
--data "events=[{"key":"[CLY]_view","count":1,"dur":DURATION,"segmentation":{"name":"VIEW_NAME"},"timestamp":TIMESTAMP,"hour":HOUR,"dow":DAYOFTHEWEEK}]"
Was this page helpful?
Reach out to us for any other questions.
Helpful?

Looking for more Help?