Narrator Docs

Viewed Page

Customer viewed a page on the website

Type: Activity Transformation

SELECT
    (pv.message_id || pv.timestamp) AS activity_id
    , pv.timestamp                  AS ts
    
    , 'segment'                     AS "source"
    , pv.anonymous_id               AS "source_id"
    
    , pv.email_address              AS "customer"
    
    , 'viewed_page'                 AS activity
    
    , pv.path                       AS feature_1 -- page_path
    , pv.referrer                   AS feature_2 -- referrer
    , CASE 
        -- derive ad source from the UTM parameters
        WHEN (gclid IS NOT NULL OR (utm_source like '%google%' and utm_medium not in ('organic', 'referral') )) THEN 'adwords'
        WHEN (fbclid IS NOT NULL OR ((utm_source like '%facebook%' OR utm_source like '%fb%') and utm_medium not in ('organic', 'referral') )) THEN 'facebook'
        WHEN (utm_source like '%linkedin%' and utm_medium not in ('organic', 'referral')) THEN 'linkedin'
        WHEN  (utm_source like '%pinterest%' and utm_medium not in ('organic', 'referral')) THEN 'pinterest'
        ELSE 'organic'
            END                     AS feature_3 -- ad_source 
    
    , NULL                          AS revenue_impact
    , pv.url                        AS link

FROM (
    select
    p.message_id
    , p.timestamp
    , p.anonymous_id
    , p.email_address
    , p.path
    , p.referrer
    , p.url
    , nullif ( substring ( regexp_substr ( lower(p.url) , 'gclid=[^&]*' ) , 7 ) , '' )  AS "gclid"
    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_source=[^&]*' ) , 12 ) , '' )  AS "utm_source"
    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_medium=[^&]*' ) , 12 ) , '' ) AS "utm_medium"
    , nullif ( substring ( regexp_substr ( lower(p.url) , 'fbclid=[^&]*' ) , 8 ) , '' ) AS fbclid
    from portal.pages AS p
    ) pv

WARNING: Segment doesn't actually guarantee that message_id is unique. If you capture and insert the message_id into another table, you will see that the same message_id is reused. We recommend that you concatenate the timestamp with a message_id to create a unique id.

Processing Configuration:

  • Run After: Started Session
    Why? Setting this transformation to run after the session will ensure that page views will be used to define sessions only AFTER the identity resolution is applied.



Started Session

A customer Started a session in your product. A session defined by google is 30 minutes of inactivity

Type: Activity Transformation

SELECT
     activity_id
     , date_add ( 'second' , -1 , ts ) AS "ts"
     , source
     , source_id
     , customer
     , 'started_session' AS "activity"
     , activity as feature_1
     , NULL AS feature_2
     , feature_3 -- ad_source
     , NULL AS revenue_impact
     , link
FROM (
    SELECT
         activity_id
         , ts
         , source
         , source_id
         , customer
         , activity
         , link
     , feature_3
         , lag (s.ts) over ( partition by nvl ( customer , source_id ) order by ts ) AS "last_ts"
    FROM narrator.activity_stream AS s
  -- activity that starts a session
    WHERE activity in ( 'viewed_page' )
) AS s

WHERE date_diff ( 'minutes' , last_ts , s.ts ) >=30 
    OR last_ts is null

Why do we subtract 1 second from the session?

We are using the first page_view to represent a session but technically a session starts right before that. We subtract 1 second to handle that case. This ensures that saying give me all page views after a session returns the first page view too.

Learn More about Creating an Activity Using Another Activity


Processing Configuration:

  • Depends On: Viewed Page
    Why? Every time the viewed page activity's definition is updated or the transformation is resynced it will also update the started session activities




Logged In

Customer logged into the platform

Type: Activity Transformation

SELECT 

    t.message_id ||t.timestamp  as activity_id
    , t.timestamp as ts

    , 'segment' AS "source"
     , t.anonymous_id AS "source_id"
    
    , u.email AS "customer"
    
    , 'logged_in' as activity
    
    , t.context_os_name as feature_1
    , t.referrer as feature_2
    , NULL as feature_3
    
    , null as revenue_impact
    , t.url as link

FROM segment.tracks t 
join internal_db.users u 
    on (u.id = p.user_id)
where t.name  = 'Logged In'



Viewed Paywall

Customer viewed a paywall alert

Type: Activity Transformation

SELECT
    t.message_id as activity_id
    , t.timestamp as ts
    
     , NULL AS "source"
     , NULL AS "source_id"
    
    , u.email AS "customer"

    , 'viewed_paywall' as activity 
    
    , d.name as feature_1 -- paywall name
    , d.reason as feature_2 -- shown reason 
    , NULL as feature_3
    
    , NULL as revenue_impact
    
    , t.context_page_url as link

FROM segment.tracks t 
join segment.paywall d
    on t.message_id = d.message_id
join internal_db.users u 
    on (u.id = p.user_id)
where label = 'Viewed Paywall'



Added to Cart

Customer viewed a paywall alert

Type: Activity Transformation

SELECT
    t.message_id as activity_id
    , t.timestamp as ts
    
     , 'segment' AS "source"
     , t.anonymous_id AS "source_id"
    
    , u.email AS "customer"

    , 'addd_to_cart' as activity 
    
    , c.sku as feature_1 -- Item SKU
    , NULL as feature_2  
    , NULL as feature_3
    
    , c.amount as revenue_impact
    
    , t.context_page_url as link

FROM segment.tracks t 
join segment.cart c
    on (c.message_id = t.message_id)

where t.label = 'Added To Cart'



Submitted Search

Customer submitted a search via the search bar

Type: Activity Transformation

SELECT
    t.message_id as activity_id
    , t.timestamp as ts
    
     , 'segment' AS "source"
     , t.anonymous_id AS "source_id"
    
    , u.email AS "customer"

    , 'submitted_search' as activity 
    
    , s.term as feature_1 -- Search Term
    , NULL as feature_2  
    , NULL as feature_3
    
    , c.amount as revenue_impact
    
    , t.context_page_url as link

FROM segment.tracks t 
join segment.search s
    on (s.message_id = t.message_id)

where t.label = 'Submitted Search'



Viewed Cart

Customer viewed the cart page

Type: Activity Transformation

SELECT
    p.message_id ||p.timestamp  as activity_id
    , p.timestamp as ts
    , 'segment' AS "source"
     , p.anonymous_id AS "source_id"
    
    , p.email AS "customer"
    
    , 'viewed_cart' as activity
    
    , p.path as feature_1
    , NULL as feature_2
    , NULL as feature_3
    
    , null as revenue_impact
    , p.url as link
    
FROM segment.pages AS p
where SPLIT_PART(p.path, '/', 3) = 'cart'



Viewed Product

Customer viewed a product

Type: Activity Transformation

SELECT
    p.message_id ||p.timestamp  as activity_id
    , p.timestamp as ts
    , 'segment' AS "source"
     , p.anonymous_id AS "source_id"
    
    , p.email AS "customer"
    
    , 'viewed_product' as activity
    
    , p.path as feature_1
    , SPLIT_PART(p.path, '/', 4) as feature_2 -- category
    , SPLIT_PART(p.path, '/', 5) as feature_3 -- sku
    
    , null as revenue_impact
    , p.url as link
    
FROM segment.pages AS p
where SPLIT_PART(p.path, '/', 3) = 'products'



Enriched Pages

Additional features for the viewed_page, viewed_product, and started_session activities


Type: Enrichment Transformation

SELECT
    p.message_id || p.timestamp AS enriched_activity_id
    , p.timestamp AS enriched_ts
    , (p.context_ip)::varchar(255) as ip_address 
    , (p.context_device_type)::varchar(255) as device
    , (p.context_page_path)::varchar(255) as page_path 
    , (p.context_referrer_link)::varchar(255) as referral_url
    , (p.context_page_title)::varchar(255) as page_title
    , nullif ( substring ( regexp_substr ( lower(p.url) ,  'fbclid=[^&]*' ) , 8 ) , '' )::varchar(255) 
        AS fbclid

    , nullif ( substring ( regexp_substr ( lower(p.url) , 'gclid=[^&]*' ) , 7 ) , '' )::varchar(255) 
         AS gclid

    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_source=[^&]*' ) , 12 ) , '' )::varchar(255) 
         AS utm_source

    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_medium=[^&]*' ) , 12 ) , '' )::varchar(255) 
         AS utm_medium

    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_campaign=[^&]*' ) , 14 ) , '' )::varchar(255) 
         AS utm_campaign

    , nullif ( substring ( regexp_substr ( lower(p.url) , 'utm_content=[^&]*' ) , 13 ) , '' )::varchar ( 255 ) 
         AS utm_content
FROM segment.pages AS p

Processing Configuration:
Enriched Activities: viewed_page, viewed_product, and started_session
Why?
All of these activities have the same activity_id because they are based on page views.



šŸ‘

Contribute to the Narrator Community

Help the Narrator community by suggesting edits or contributing SQL transformations. Use the "Suggest Edits" button at the top right or visit our Contributors page for more info on how to get involved.




Have questions?


Our data team is here to help! Here are a couple ways to get in touch...

šŸ’¬ Chat us from within Narrator

šŸ’Œ Email us at [email protected]

šŸ—“ Or schedule a 15-min meeting with our data team

Updated 2 months ago


What's Next

Don't see what you're looking for? Browse the Activity Index for more examples...

Activity Index

Segment


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.