Path analysis functions
Hologres provides a set of path analysis functions, including path detail functions and path parsing functions. These functions efficiently process path computation tasks, calculate path details, and parse results. You can use the recorded data to generate a Sankey diagram, which helps you understand and visualize complex path information. This topic describes how to use path detail and path parsing functions.
Background
Path analysis records the distribution of user paths and the sequence of actions in each session when users interact with your product or feature. It generates an intuitive Sankey diagram of user behavior. The Sankey diagram displays each key node in the user flow and the traffic between these nodes, which enables granular business analysis. By using path analysis, you can clearly understand user engagement with each key feature of your product. This helps your operations and product teams optimize business strategies and iterate on the product, driving business growth. Hologres supports a path detail function for detailed path computation and a path parsing function for result analysis. The output can be used directly to create a Sankey diagram.
The top of the path analysis Sankey diagram interface provides a time zone selector, a time range selector, settings for the maximum number of nodes per layer, and an Export button. The Sankey diagram starts from an initial event (such as $AppStart) and progressively displays subsequent user actions (like $AppEnd, AppPageview, Search, CheckIn, and AllPayments). Each node is labeled with the event name and its trigger count, and the width of the flow between nodes reflects the traffic volume.
Usage notes
-
Path analysis functions are available in Hologres V2.2 and later. If your instance is V2.1 or earlier, contact Hologres technical support to upgrade your instance.
-
To use path analysis functions, you must create an extension. By default, the extension is loaded into the public schema and cannot be loaded into other schemas.
--Create the extension. CREATE extension flow_analysis;NoteAn extension is database-level and only needs to be created once per database.
Path detail functions
Path detail function (path_analysis_detail)
The path_analysis_detail function analyzes specified event data and presents it as a detailed path structure. The output is a serialized array that contains comprehensive information, including the complete path sequence, the parent-child relationships between nodes in the path, and the duration of each step.
-
Syntax
path_analysis_detail( event, event_time, start_event, session_interval_sec, path_depth, path_offset, is_reverse, split_session_by_event) -
Parameters
Parameter
Type
Description
event
text (data)
The event to be analyzed.
event_time
timestamp, timestamptz, bigint (data)
The timestamp of each event.
start_event
text (literal)
The specified start or end event.
session_interval_sec
bigint (literal)
The session interval in seconds (s).
If the time between adjacent events in a session exceeds the specified session interval, the system splits it into two sessions. For example, if you set the interval to 30s and the time between two events is greater than 30s, the events are considered parts of two different sessions.
path_depth
bigint (literal)
The number of events to retrieve in the sequence, starting from the specified start or end event.
path_offset
bigint (literal)
The event offset from the start or end event. A value of 0 means no offset.
For example, for an event series a, b, and c, if a is the start event and the offset is 1, matching starts from event b.
is_reverse
bool (literal)
The output order of the event series. Valid values:
-
false: Specifies a start event. The event series is returned in chronological order.
-
true: Specifies an end event. The event series is returned in reverse chronological order.
split_session_by_event
bool (literal)
The method for session splitting. Valid values:
-
true: A new session starts whenever the specified start or end event is encountered in the event series.
-
false (Default): Sessions are split only based on the value of the
session_interval_secparameter.NoteIf
session_interval_secis set to -1, sessions are split by event instead of time.
For example, if a path such as
abcabc...abcis repeated 10 times within the same session, the function's matching mechanism records only a single conversion. If you want to treat each repetition of the path as a new session to calculate each conversion independently, you can set this parameter totrue. This allows each new operation to be recorded and counted separately. -
-
Return value
path_analysis_detail: Returns a value of the
textdata type. Example:{"",\x01a\x01b<,\x01b\x01c<,""}.NoteTo read the result, you must use a corresponding path parsing function.
pad_funnel function
The pad_funnel function retrieves subpath information for a specific combination of events.
-
Syntax
pad_funnel(path_analysis_detail(), target_path) -
Parameters
Parameter
Type
Description
path_analysis_detail()
text (data)
The result returned by the
path_analysis_detailfunction.target_path
text (literal)
The specified event series.
-
Return value
pad_funnel: Returns a
textvalue. Example:text []{"",\x01a\x01b<,\x01b\x01c<,""}.NoteTo read the result, you must use a corresponding path parsing function.
Path parsing functions
pad_full_path function
The pad_full_path function parses the result of path_analysis_detail into parent paths, subpaths, and the duration of each step.
-
Syntax
pad_full_path(path_analysis_detail()) -
Parameters
path_analysis_detail(): The result returned by the
path_analysis_detailfunction. -
Return value
-
The
pad_full_pathfunction returns the following content.pad_sub_path_left(unnested_pad_result) pad_sub_path_right(unnested_pad_result) pad_sub_index_left(unnested_pad_result) pad_sub_index_right(unnested_pad_result) pad_sub_cost(unnested_pad_result) pad_sub_session(unnested_pad_result) -
The following table describes the return parameters.
Function
Type
Description
pad_sub_path_left
text
The start event of the subpath.
pad_sub_path_right
text
The end event of the subpath.
pad_sub_index_left
bigint
The index of the subpath's start event within the complete path of the session.
pad_sub_index_right
bigint
The index of the subpath's end event within the complete path of the session.
pad_sub_cost
bigint
The transition duration for the subpath, in seconds (s).
pad_sub_session
bigint
The index of the valid session to which the subpath belongs.
-
pad_session_path_array function
The pad_session_path_array function extracts the event series for a specified session ID as an array.
-
Syntax
pad_session_path_array(path_analysis_detail(), session_idx) -
Parameters
-
path_analysis_detail(): The result returned by the
path_analysis_detailfunction. -
session_idx: The specified session index.
-
-
Return value
Returns data of the array type.
Examples
Prepare data
--Create the extension if it does not already exist.
CREATE extension flow_analysis;
--Prepare the data.
CREATE TABLE path_demo(
uid text,
event text,
event_time timestamptz
);
INSERT INTO path_demo VALUES
('1','Register','2023-11-24 16:01:23+08'),
('1','Log on','2023-11-24 16:02:10+08'),
('1','Browse','2023-11-24 16:02:15+08'),
('1','View live streams','2023-11-24 16:03:10+08'),
('1','Browse','2023-11-24 16:03:15+08'),
('1','Add to favorites','2023-11-24 16:04:20+08'),
('1','Browse','2023-11-24 16:07:21+08'),
('1','Purchase','2023-11-24 16:08:23+08'),
('1','Exit','2023-11-24 16:09:05+08'),
('2','Log on','2023-11-24 16:10:23+08'),
('2','Purchase','2023-11-24 16:12:23+08'),
('3','Log on','2023-11-24 16:02:23+08'),
('3','Browse','2023-11-24 16:02:23+08'),
('3','Add to favorites','2023-11-24 16:03:53+08'),
('3','View live streams','2023-11-24 16:04:53+08'),
('4','Log on','2023-11-24 16:02:23+08'),
('4','Browse','2023-11-24 16:03:53+08'),
('4','Purchase','2023-11-24 16:04:23+08'),
('4','View live streams','2023-11-24 16:05:53+08'),
('4','Cancel the order','2023-11-24 16:06:53+08');
Example 1: Recording all event paths
-
Split sessions by time.
--Split by time: Specify 'Log on' as the start event, set the session interval to 180 s, and set the matching sequence length to 7. Then, use the pad_full_path function to decode the results. SELECT uid, pad_full_path(path_analysis_detail(event, event_time, 'Log on', 180, 7, 0, false)) AS ret FROM path_demo GROUP BY uid;The following result is returned.
uid | ret -----+--------------------------------------------------- 3 | {Log on->Add to favorites->View live streams} 4 | {Log on->Browse->Purchase->View live streams->Cancel the order} 1 | {Log on->Browse->View live streams->Browse->Add to favorites} 2 | {Log on->Purchase} (4 rows) -
Split sessions by time and event.
--Split by time and event: Specify 'Browse' as the start event, set the interval to 180 s, and set the sequence length to 7. Then, use the pad_full_path function to decode the results. SELECT uid, pad_full_path(path_analysis_detail(event, event_time, 'Browse', 180, 7, 0, false,TRUE)) AS ret FROM path_demo GROUP BY uid;The following result is returned.
uid | ret -----+----------------------------------------------------------- 1 | {Browse->View live streams,Browse->Add to favorites,Browse->Purchase->Exit} 2 | {} 4 | {Browse->Purchase->View live streams->Cancel the order} 3 | {Browse->Log on->Add to favorites->View live streams}
Example 2: Expanding the path result
--Expand the paths.
SELECT uid, unnest(pad_full_path(path_analysis_detail(event, event_time, 'Log on', 180, 7, 0, false))) AS ret FROM path_demo GROUP BY uid;
For more information about the UNNEST function, see UNNEST clause.
The following result is returned.
uid | ret
-----+-------------------------------------------------
3 | Log on->Add to favorites->View live streams
1 | Log on->Browse->View live streams->Browse->Add to favorites
2 | Log on->Purchase
4 | Log on->Browse->Purchase->View live streams->Cancel the order
(4 rows)
Example 3: Expand subpath details
--Expand the subpaths.
SELECT
uid,
pad_sub_session (ret) AS session_id,
pad_sub_path_left (ret) AS sub_path_left,
pad_sub_path_right (ret) AS sub_path_right,
pad_sub_index_left (ret) AS sub_index_left,
pad_sub_index_right (ret) AS sub_index_right,
pad_sub_cost (ret) AS sub_cost
FROM (
SELECT
uid,
unnest( path_analysis_detail (event, event_time, 'Log on', 180, 7, 0, FALSE)) AS ret
FROM
path_demo
GROUP BY
uid) a ;
The following result is returned.
uid | session_id | sub_path_left | sub_path_right | sub_index_left | sub_index_right | sub_cost
-----+------------+---------------+----------------+----------------+-----------------+----------
1 | 0 | | Log on | -1 | 0 | 0
1 | 0 | Log on | Browse | 0 | 1 | 5
1 | 0 | Browse | View live streams | 1 | 2 | 55
1 | 0 | View live streams | Browse | 2 | 3 | 5
1 | 0 | Browse | Add to favorites | 3 | 4 | 65
2 | 0 | | Log on | -1 | 0 | 0
2 | 0 | Log on | Purchase | 0 | 1 | 120
3 | 0 | | Log on | -1 | 0 | 0
3 | 0 | Log on | Add to favorites | 0 | 1 | 90
3 | 0 | Add to favorites | View live streams | 1 | 2 | 60
4 | 0 | | Log on | -1 | 0 | 0
4 | 0 | Log on | Browse | 0 | 1 | 90
4 | 0 | Browse | Purchase | 1 | 2 | 30
4 | 0 | Purchase | View live streams | 2 | 3 | 90
4 | 0 | View live streams | Cancel the order | 3 | 4 | 60
(15 rows)
Example 4: Get session event series
SELECT
uid,
pad_session_path_array (path_analysis_detail (event, event_time, 'Log on', 180, 7, 0,FALSE), 0) AS ret
FROM
path_demo
GROUP BY
uid;
The following result is returned.
uid | ret
-----+-----------------------------------------------
1 | {Log on,Browse,View live streams,Browse,Add to favorites}
2 | {Log on,Purchase}
3 | {Log on,Add to favorites,View live streams}
4 | {Log on,Browse,Purchase,View live streams,Cancel the order}
(4 rows)
Example 5: Calculate subpath PV and UV
--Calculate the page view (PV) and unique visitor (UV) for each subpath without deduplication. If you need to deduplicate the results, you can perform deduplication on the uid.
SELECT
sub_index,
sub_path_left,
sub_path_right,
count(uid)
FROM (
SELECT
uid,
pad_sub_path_left (ret) AS sub_path_left,
pad_sub_path_right (ret) AS sub_path_right,
pad_sub_index_right (ret) AS sub_index
FROM (
SELECT
uid,
unnest(path_analysis_detail (event, event_time, 'Log on', 180, 7, 0, FALSE)) AS ret
FROM
path_demo
GROUP BY
uid) a) a
GROUP BY
sub_index,
sub_path_left,
sub_path_right
ORDER BY
sub_index,
sub_path_left,
sub_path_right;
The following result is returned.
sub_index | sub_path_left | sub_path_right | count
-----------+---------------+----------------+-------
0 | | Log on | 4
1 | Log on | Add to favorites | 1
1 | Log on | Browse | 2
1 | Log on | Purchase | 1
2 | Add to favorites | View live streams | 1
2 | Browse | View live streams | 1
2 | Browse | Purchase | 1
3 | View live streams | Browse | 1
3 | Purchase | View live streams | 1
4 | Browse | Add to favorites | 1
4 | View live streams | Cancel the order | 1
(11 rows)
Example 6: Calculate average subpath duration
--Calculate the average duration for each subpath.
SELECT
sub_path_left,
sub_path_right,
avg(sub_cost)
FROM (
SELECT
uid,
pad_sub_path_left (ret) AS sub_path_left,
pad_sub_path_right (ret) AS sub_path_right,
pad_sub_cost (ret) AS sub_cost
FROM (
SELECT
uid,
unnest(path_analysis_detail (event, event_time, 'Log on', 180, 7, 0, FALSE)) AS ret
FROM
path_demo
GROUP BY
uid) a) a
GROUP BY
sub_path_left,
sub_path_right
ORDER BY
sub_path_left,
sub_path_right;
The following result is returned.
sub_path_left | sub_path_right | avg
---------------+----------------+------------
Add to favorites | View live streams | 60.000000
Browse | Add to favorites | 65.000000
Browse | View live streams | 55.000000
Browse | Purchase | 30.000000
Log on | Add to favorites | 90.000000
Log on | Browse | 47.500000
Log on | Purchase | 120.000000
View live streams | Cancel the order | 60.000000
View live streams | Browse | 5.000000
Purchase | View live streams | 90.000000
| Log on | 0.000000
(11 rows)
Example 7: Associating session paths with subpath details
--Associate session paths with subpaths.
select
uid,
pad_sub_session(item) as session_id,
full_path [pad_sub_session(item)+1] as full_path,
pad_sub_path_left(item) as sub_path_left,
pad_sub_path_right(item) as sub_path_right,
pad_sub_index_right(item) as sub_idx,
pad_sub_cost(item) as sub_cost
from
(
select
uid,
unnest(ret) as item,
pad_full_path(ret) as full_path
from
(
select
uid,
path_analysis_detail(event, event_time, 'Log on', 180, 7, 0, false) as ret
from
path_demo
group by
uid
) a
) a;
The following result is returned.
uid | session_id | full_path | sub_path_left | sub_path_right | sub_idx | sub_cost
-----+------------+-------------------------------------------------+---------------+----------------+---------+----------
3 | 0 | Log on->Add to favorites->View live streams | | Log on | 0 | 0
3 | 0 | Log on->Add to favorites->View live streams | Log on | Add to favorites | 1 | 90
3 | 0 | Log on->Add to favorites->View live streams | Add to favorites | View live streams | 2 | 60
1 | 0 | Log on->Browse->View live streams->Browse->Add to favorites | | Log on | 0 | 0
1 | 0 | Log on->Browse->View live streams->Browse->Add to favorites | Log on | Browse | 1 | 5
1 | 0 | Log on->Browse->View live streams->Browse->Add to favorites | Browse | View live streams | 2 | 55
1 | 0 | Log on->Browse->View live streams->Browse->Add to favorites | View live streams | Browse | 3 | 5
1 | 0 | Log on->Browse->View live streams->Browse->Add to favorites | Browse | Add to favorites | 4 | 65
2 | 0 | Log on->Purchase | | Log on | 0 | 0
2 | 0 | Log on->Purchase | Log on | Purchase | 1 | 120
4 | 0 | Log on->Browse->Purchase->View live streams->Cancel the order | | Log on | 0 | 0
4 | 0 | Log on->Browse->Purchase->View live streams->Cancel the order | Log on | Browse | 1 | 90
4 | 0 | Log on->Browse->Purchase->View live streams->Cancel the order | Browse | Purchase | 2 | 30
4 | 0 | Log on->Browse->Purchase->View live streams->Cancel the order | Purchase | View live streams | 3 | 90
4 | 0 | Log on->Browse->Purchase->View live streams->Cancel the order | View live streams | Cancel the order | 4 | 60
(15 rows)
Example 8: View partial path details
--View conversion details for a specific path ('Log on' to 'Purchase') using the pad_funnel function.
SELECT uid, pad_full_path(pad_funnel(path_analysis_detail(event, event_time, 'Log on', 180, 7, 0, false), array['Log on', 'Purchase'])) AS ret FROM path_demo GROUP BY uid;
The following result is returned.
uid | ret
-----+------------------
3 | {Log on}
4 | {Log on->Purchase}
1 | {Log on}
2 | {Log on->Purchase}
(4 rows)