Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

measure average time of users who have made a purchase in GA4

Hi do you know howe we can exactly measure average time of users who have made a purchase in GA4

 

0 2 482
2 REPLIES 2

AndrewB
Community Manager
Community Manager

You might try something like this: 

-- Step 1: Identify sessions with purchases within a specific timeframe
WITH sessions_with_purchases AS (
    SELECT DISTINCT
        session_id  -- Use DISTINCT to avoid counting the same session multiple times 
                     -- if there are multiple purchase events in a single session 
    FROM `your-project-id.analytics_xxxx.events_*`
    WHERE event_name = 'purchase'
      AND event_timestamp BETWEEN 
          TIMESTAMP_MICROS(your_start_date_in_microseconds)  -- Replace with your actual date range
          AND TIMESTAMP_MICROS(your_end_date_in_microseconds)
),

-- Step 2: Calculate session duration for each session
session_durations AS (
  SELECT
    session_id,
    -- For GA4, you can often use the built-in 'session_engaged' event
    -- to get more accurate engagement time:
    MAX(CASE 
          WHEN event_name = 'session_engaged' THEN event_timestamp 
          ELSE NULL  -- Ignore other event types for duration calculation
        END) - 
    MIN(event_timestamp) AS session_duration_microseconds 
  FROM `your-project-id.analytics_xxxx.events_*`
  WHERE event_timestamp BETWEEN 
      TIMESTAMP_MICROS(your_start_date_in_microseconds)
      AND TIMESTAMP_MICROS(your_end_date_in_microseconds)
  GROUP BY session_id
)

-- Step 3: Join to get durations for sessions with purchases and calculate the average
SELECT
  AVG(sd.session_duration_microseconds / 1000000) AS avg_session_duration_seconds 
FROM sessions_with_purchases swp
JOIN session_durations sd ON swp.session_id = sd.session_id;

Thank you but i dont understand where i can find this 

 TIMESTAMP_MICROS(your_start_date_in_microseconds)  -- Replace with your actual date range
          AND TIMESTAMP_MICROS(your_end_date_in_microseconds)

Where i need to go to find this? Thank you