Skip to content

Login Days

Scenario

It can be useful to calculate the number of days since a visitors last login, storing this value for further use in reports / dimensions. It can be useful for:

  • User Activity Tracking: This function is useful for tracking user activity, specifically how long it has been since a user last logged in.
  • Engagement Metrics: It can help in generating engagement metrics, identifying inactive users, and triggering re-engagement campaigns.
  • Security Monitoring: It can be used for security purposes, such as flagging accounts that haven't been accessed for a long time.

Question

This example is to calculate Login days, but could there be other Login related events for reporting? Login attempts? or Login duration?

Example Event Script

Example Adv. Event
// Generated by Discover Event Manager
// NOTE: Do not change event name

function CUST$E_AOB_COLLECT_LAST_LOGIN_NUMBER_OF_DAYS_E_FPS_NUM__635479788683942502() {
    if ($P["CUST.P_AOB_COLLECT_LAST_LOGIN_DATE_AND_TIME__635479788582694608"].patternFound() && $H.URL.toUpperCase() === "/IDPPROXY/EXECUTOR/CUSTOMER") {
        const origDate = new Date($P["CUST.P_AOB_COLLECT_LAST_LOGIN_DATE_AND_TIME__635479788582694608"].firstValue());
        const today = new Date();
        today.setHours(0, 0, 0, 0);

        const diffInMs = Math.abs(today - origDate);
        const daysDiff = Math.floor(diffInMs / (24 * 60 * 60 * 1000));

        $F.setFact("CUST.F_E_AOB_COLLECT_LAST_LOGIN_NUMBER_OF_DAYS_E_FPS_NUM__635479788683942502", daysDiff);
    }
}

Tips

  1. As with all advanced events, it is best practice to create the event using the UI as much as possible and only converting to Advanced Mode when additional scripting is required. This is particularly useful when several conditions are required.
  2. The function name and number/id is generated at creation time and should not be modified. Any modification without a full event re-creation will have undesired results.

The function first checks if a specific pattern $P["CUST.P_AOB_COLLECT_LAST_LOGIN_DATE_AND_TIME__635479788582694608"].patternFound() is found in the user's last login date and time data, along with ensuring the current URL $H.URL.toUpperCase() === "/IDPPROXY/EXECUTOR/CUSTOMER" in the event matches a specific target URL (/IDPPROXY/EXECUTOR/CUSTOMER).

Date Calculation:

  • Retrieves the original login date.
  • Sets the current date to midnight (00:00:00) to ensure accurate day difference calculation.
  • Calculates the absolute difference in milliseconds between the current date and the original login date.
  • Converts this difference from milliseconds to days.

Finally, the daysDiff stores the calculated number of days since the last login in a fact for reporting purposes.