{"id":21922649,"url":"https://github.com/oguzgn/a-case-study-for-a-livestreaming-platform","last_synced_at":"2025-06-23T15:36:41.562Z","repository":{"id":265142821,"uuid":"895264643","full_name":"oguzgn/a-case-study-for-a-livestreaming-platform","owner":"oguzgn","description":"This project aims to analyze livestream watch times of users across different regions. The goal is to identify the top 5 users with the highest watch time for each region. The analysis involves multiple SQL transformations to extract meaningful insights from the data.","archived":false,"fork":false,"pushed_at":"2024-11-27T22:10:35.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T10:51:33.632Z","etag":null,"topics":["bigquery","data","data-analysis","data-modeling","live-streaming","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oguzgn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-27T21:52:20.000Z","updated_at":"2024-12-02T14:08:21.000Z","dependencies_parsed_at":"2024-11-27T22:41:57.347Z","dependency_job_id":null,"html_url":"https://github.com/oguzgn/a-case-study-for-a-livestreaming-platform","commit_stats":null,"previous_names":["oguzgn/a-live-stream-platform-case-study"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/oguzgn/a-case-study-for-a-livestreaming-platform","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oguzgn%2Fa-case-study-for-a-livestreaming-platform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oguzgn%2Fa-case-study-for-a-livestreaming-platform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oguzgn%2Fa-case-study-for-a-livestreaming-platform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oguzgn%2Fa-case-study-for-a-livestreaming-platform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oguzgn","download_url":"https://codeload.github.com/oguzgn/a-case-study-for-a-livestreaming-platform/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oguzgn%2Fa-case-study-for-a-livestreaming-platform/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261505458,"owners_count":23169040,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bigquery","data","data-analysis","data-modeling","live-streaming","sql"],"created_at":"2024-11-28T21:05:49.641Z","updated_at":"2025-06-23T15:36:41.508Z","avatar_url":"https://github.com/oguzgn.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Livestream Watch Time Analysis\n\n## Project Overview\n\nThis project aims to analyze livestream watch times of users across different regions. The goal is to identify the top 5 users with the highest watch time for each region. The analysis involves multiple SQL transformations to extract meaningful insights from the data.\n\n## Steps in the Query\n\n### 1. Livestream Watch Time Calculation\nIn this step, we calculate the time each user spent in a livestream session.  \n- Combine the `livestream_enter` and `livestream_exit` tables using a `LEFT JOIN`.  \n- For users who are still in the livestream, use the current timestamp as their `exit_time`.  \n- Ensure that `exit_time` is always greater than or equal to `enter_time`.  \n- Group by the `user_id`, `livestream_id`, and `enter_time` to compute session-specific watch times.\n\n```sql\nWITH LivestreamWatchTime AS (\n    SELECT\n        le.user_id,\n        le.livestream_id,\n        le.event_time AS enter_time,\n        COALESCE(MIN(le_exit.event_time), CURRENT_TIMESTAMP()) AS exit_time\n    FROM xxx.livestream_enter le\n    LEFT JOIN xxx.livestream_exit le_exit ON le.user_id = le_exit.user_id\n        AND le.livestream_id = le_exit.livestream_id\n        AND le_exit.event_time \u003e= le.event_time\n    GROUP BY le.user_id, le.livestream_id, le.event_time\n)\n```\n\n### 2. Region Mapping\nHere, we map users to their respective regions based on their app entry times.  \n\n- Use the `user_entry` table to determine the region for each user session.  \n- Ensure that the `livestream_enter` time is greater than or equal to the `user_entry` time.  \n- Use the `LEAD` function to calculate the next entry time, which helps identify the active region during overlapping sessions.  \n\n```sql\nUserRegion AS (\n    SELECT\n        ue.user_id,\n        ue.event_time AS entry_time,\n        ue.region,\n        LEAD(ue.event_time) OVER (PARTITION BY ue.user_id ORDER BY ue.event_time) AS next_entry_time\n    FROM xxx.user_entry ue\n)\n```\n\n### 3. Combine Watch Time with Region\nIn this step, we combine the watch time data with region information.  \n\n- Join the `LivestreamWatchTime` and `UserRegion` tables to associate each livestream session with the correct region.  \n- Ensure that the `livestream_enter` time falls between the `entry_time` and the `next_entry_time` for the region.  \n\n```sql\nWatchTimeWithRegion AS (\n    SELECT\n        lwt.user_id,\n        ur.region,\n        TIMESTAMP_DIFF(lwt.exit_time, lwt.enter_time, SECOND) / 60 AS watch_time_minutes\n    FROM LivestreamWatchTime lwt\n    LEFT JOIN UserRegion ur ON lwt.user_id = ur.user_id\n        AND lwt.enter_time \u003e= ur.entry_time\n        AND (lwt.enter_time \u003c ur.next_entry_time OR ur.next_entry_time IS NULL)\n)\n```\n\n### 4. Total Watch Time by Region and User\nNext, we calculate the total watch time for each user in each region.  \n\n- Group the data by `region` and `user_id`.  \n- Sum up the watch time in minutes for all sessions.  \n\n```sql\nTotalWatchTime AS (\n    SELECT\n        region,\n        user_id,\n        SUM(watch_time_minutes) AS total_watch_time_minutes\n    FROM WatchTimeWithRegion\n    GROUP BY region, user_id\n)\n```\n\n### 6. Retrieve Top 5 Users per Region\nFinally, we extract the top 5 users with the highest watch times in each region.  \n\n- Filter the ranked data to include only users with ranks 1 through 5.  \n- Sort the results by `region` and descending `total_watch_time_minutes`.  \n\n```sql\nSELECT\n    region AS Region,\n    user_id AS UserID,\n    total_watch_time_minutes AS TotalWatchTimeMinutes\nFROM RankedWatchTime\nWHERE rank \u003c= 5\nORDER BY region ASC, total_watch_time_minutes DESC;\n```\n\n## Full Query\nThe full query integrates all the steps outlined above, with the aim to retrieve the top 5 users by total watch time in each region. You can find the full SQL code in the main `final-query`.\n\nThe logic involves the following key steps:\n1. **LivestreamWatchTime:** This table captures each user's entry and exit time for livestreams.\n2. **UserRegion:** Maps users to their regions based on entry times, and calculates the next region entry to handle overlapping sessions.\n3. **WatchTimeWithRegion:** Combines the watch time data with regions to calculate total minutes watched by each user in each region.\n4. **TotalWatchTime:** Aggregates the total watch time for each user per region.\n5. **RankedWatchTime:** Ranks users within each region based on their total watch time in descending order.\n6. **Final Output:** Filters the top 5 ranked users per region and outputs their details.\n\n\n### Expected Output\nThe query produces a table with the following columns:\n- **Region:** The region where the user accessed the livestream.\n- **UserID:** The unique identifier for the user.\n- **TotalWatchTimeMinutes:** The total watch time (in minutes) for the user in the region.\n\nYou can then use the results to identify the top users per region, which is useful for targeting specific regions or rewarding high-engagement users.\n\n## Use Case\nThis query is designed for analysis in live streaming platforms or applications that track user interactions across different regions. It helps:\n- Identify the most engaged users in each region.\n- Tailor content, advertisements, or rewards to specific regions with high user engagement.\n- Optimize livestream strategies by focusing on regions or users that contribute the most watch time.\n\n## Conclusion\nThis project provides valuable insights into user behavior during livestream events, specifically by identifying the top users in each region based on their total watch time. The query can be customized further to meet your specific needs, such as adjusting the number of top users or including additional user metadata.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foguzgn%2Fa-case-study-for-a-livestreaming-platform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foguzgn%2Fa-case-study-for-a-livestreaming-platform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foguzgn%2Fa-case-study-for-a-livestreaming-platform/lists"}