Built with datascienceportfol.io

Website Traffic Data

Project photo

Skills

About this project

USE Others
 
------Query 1 Session Duration & Page Views - Device Type
Select 
 dl.device_type as 'Device Type',
 SUM(wtd.Session_Duration_Seconds)/3600 as 'Total Session Duration(Hrs)',
 CAST(AVG(wtd.Session_Duration_Seconds)as decimal(10,2))/3600 as 'Average Session Duration(Hrs)',
 SUM(wtd.Page_Views_Per_Session) as 'Total Page Views'
from website_traffic_data wtd
JOIN Device_Lookup dl ON wtd.Device_Key = dl.Device_Key
Group by dl.Device_Type
Order by dl.Device_Type desc
 
 
------Query 2 Session Duration & Page Views - Traffic Source
Select 
 sl.source_type as 'Source_Type',
 SUM(wtd.Session_Duration_Seconds)/3600 as 'Total Session Duration(Hrs)',
 CAST(AVG(wtd.Session_Duration_Seconds)as decimal(10,2))/3600 as 'Average Session Duration(Hrs)',
 SUM(wtd.Page_Views_Per_Session) as 'Total Page Views'
from website_traffic_data wtd
JOIN Source_Lookup sl
  ON wtd.Source_Key = sl.Source_Key
Group by sl.Source_Type
Order by sl.Source_Type desc
 
 
--Query 3 Content Segment by Bounce_Rate
 With CTE
AS
(
 Select dl.Content_Segment as Content_Segment,
       COUNT(wtd.Session_Id)  AS Total_Sessions
from Website_Traffic_Data wtd
JOIN Device_Lookup dl 
 ON wtd.Device_Key = dl.Device_Key
Group by dl.Content_Segment    
)
 
Select dl.Content_Segment,
       COUNT(CAST(Session_Id As decimal(10,2))) / MAX(CAST(CTE.total_sessions AS decimal(10,2))) * 100 as Bounced_Rate
from Website_Traffic_Data wtd
JOIN Device_Lookup dl 
 ON wtd.Device_Key = dl.Device_Key 
JOIN CTE on CTE.Content_Segment = dl.Content_Segment
where wtd.Page_Views_Per_Session < 2
group by dl.Content_Segment
 
 
 
 --Query 4 Session Duration & Page_Views by Device_Type
 With CTE_SDDT
 AS
  (Select dl.device_type as Device_Type,
        CAST(SUM(wtd.Session_Duration_Seconds)AS decimal(10,1))/3600 as Total_Session_Duration,
        CAST(AVG(wtd.Session_Duration_Seconds)AS decimal(10,2)) / 3600 as Average_Session,
SUM(Page_Views_Per_Session) as Total_Views
 from Website_Traffic_Data wtd
 JOIN Device_Lookup dl 
  ON wtd.Device_Key = dl.Device_Key
  Group by dl.Device_Type
)
 
Select Device_Type,
      Total_Session_Duration,
  Average_Session,
  Total_Views
from CTE_SDDT
 
 
---Query 5 Bounce Rate by Website Traffic Sources
With cte1 
AS
(
  Select 
    sl.source_type,
COUNT(wtd.session_id) AS total_session
FROM
Website_Traffic_Data wtd
INNER JOIN Source_Lookup sl 
ON wtd.Source_Key = sl.Source_Key
  Group by sl.Source_Type
)
 
Select 
 sl.source_type,
 COUNT(CAST(SESSION_ID as decimal(10,2))) / max(CAST(cte1.total_session as decimal(10,2)))  * 100 as Bounced_rate
 FROM
  Website_Traffic_Data wtd
JOIN Source_Lookup sl 
 ON sl.Source_Key = wtd.Source_Key
JOIN cte1 ON cte1.source_type = sl.Source_Type
WHERE
wtd.Page_Views_Per_Session < 2
group by sl.Source_Type
Order by Bounced_rate desc
 
--Query 6 Bounce Rate by Device_Type
With cte
AS
(
  Select dl.device_type,
         COUNT(wtd.Session_Id) AS Total_Session
  from Website_Traffic_Data wtd
JOIN Device_Lookup dl 
 ON wtd.Device_Key = dl.Device_Key
Group by dl.Device_Type
)
 
 Select dl.Device_Browser,
        COUNT(Session_Id) / MAX(cte.Total_Session) * 100 AS Bounced_Rate
 from Website_Traffic_Data wtd
 JOIN Device_Lookup dl
  ON wtd.Device_Key = dl.Device_Key
 JOIN cte ON dl.Device_Type = cte.device_type
 Group by dl.Device_Browser
 
---Query 7 Bounce Rate by Device_Browser
With cte1 
AS
(
  Select 
    dl.device_browser,
COUNT(wtd.session_id) AS total_session
FROM
Website_Traffic_Data wtd
INNER JOIN Device_Lookup dl 
ON wtd.Device_Key = dl.Device_Key
  Group by dl.Device_Browser
)
 
Select 
 dl.device_browser,
 COUNT(CAST(SESSION_ID as decimal(10,2))) / max(CAST(cte1.total_session as decimal(10,2))) * 100 as Bounced_rate
 FROM
  Website_Traffic_Data wtd
JOIN Device_Lookup dl 
 ON dl.Device_Key = dl.Device_Key
JOIN cte1 ON cte1.device_browser = dl.Device_Browser
WHERE
  wtd.Page_Views_Per_Session < 2
group by dl.Device_Browser
 
 
--------------------------------------------------------------TREND ANALYSIS--------------------------------------------------------------------------------------
----Query 1 Total Session based on device types
 Select 
   DATEPART(YEAR,wtd.Date_Key) AS YEAR,
   DATEPART(QUARTER,wtd.Date_Key) AS QUARTER,
   dl.device_type,
   CAST(SUM(wtd.Session_Duration_Seconds)AS decimal(10,2)) / 3600 as Total_Session_in_hrs
 from Website_Traffic_Data wtd
 JOIN Device_Lookup dl 
  ON wtd.Device_Key = dl.Device_Key
  Group by 
    DATEPART(YEAR,wtd.Date_Key),
    DATEPART(QUARTER,wtd.Date_Key),dl.Device_Type
  Order by 
           dl.Device_Type,
   DATEPART(YEAR,wtd.Date_Key),
           DATEPART(QUARTER,wtd.Date_Key)
 
---Query 2 ----Total Session(hrs) based on traffic source
 Select 
       DATEPART(YEAR,wtd.Date_Key) as YEAR,
   DATEPART(QUARTER,wtd.Date_Key) as Quarter,
   sl.source_type as traffic_source,
   SUM(wtd.session_duration_seconds) / 3600 as Total_Duration_Hrs
 from Website_Traffic_Data wtd
 JOIN Source_Lookup sl ON wtd.Source_Key = sl.Source_Key
 Group by
        DATEPART(YEAR,wtd.Date_Key),
   DATEPART(QUARTER,wtd.Date_Key),
   sl.source_type
 Order by sl.Source_Type, DATEPART(YEAR,wtd.Date_Key),
                            DATEPART(QUARTER,wtd.Date_Key) 
 
---Query 3 Average Session Duration (Overall Trend)
 Select 
   DATEPART(YEAR,date_key) as YEAR,
   DATEPART(QUARTER,date_key) as QUARTER,
   CAST(AVG(Session_Duration_Seconds)AS decimal(10,2)) / 3600 as Average       
 from Website_Traffic_Data
 GROUP BY  DATEPART(YEAR,date_key),
   DATEPART(QUARTER,date_key) 
 Order by  DATEPART(YEAR,date_key),
   DATEPART(QUARTER,date_key)
 
--Query 4 Bounce Rate - Monthly Trend
With cte1 
AS
(
  Select 
    DATENAME(MONTH,date_key) as MONTHNAME,
COUNT(session_id) AS total_session
FROM
Website_Traffic_Data wtd
Group by DATENAME(MONTH,date_key) 
)
 
Select 
 DATENAME(MONTH,wtd.date_key) as MONTH_NAME,
 COUNT(CAST(wtd.Session_Id as decimal(10,2))) / max(CAST(cte1.total_session as decimal(10,2))) * 100 as Bounced_rate
 FROM
  Website_Traffic_Data wtd
JOIN cte1 ON cte1.MONTHNAME =  DATENAME(MONTH,date_key)
WHERE
  wtd.Page_Views_Per_Session < 2
Group by DATENAME(MONTH,wtd.date_key)
Order by MONTH_NAME 
 
 
 
 
--Query 5 Bounce Rate - Daily Trend
With cte1 
AS
(
  Select 
    DATENAME(WEEKDAY,date_key) as DAYNAME,
COUNT(session_id) AS total_session
FROM
Website_Traffic_Data wtd
Group by DATENAME(WEEKDAY,date_key) 
)
 
Select 
 DATENAME(WEEKDAY,date_key) as DAY_NAME,
 COUNT(CAST(wtd.Session_Id AS decimal(10,2))) / max(CAST(cte1.total_session AS decimal(10,2))) * 100 as Bounced_rate
 FROM
  Website_Traffic_Data wtd
JOIN cte1 ON cte1.DAYNAME =  DATENAME(WEEKDAY,date_key)
WHERE
  wtd.Page_Views_Per_Session < 2
Group by DATENAME(WEEKDAY,wtd.date_key), DATENAME(WEEKDAY,date_key) 
Order by DAY_NAME 
 
 
--------Georgraphical Analysis---------------------------------------------
---Query 1 Total Page Views by RegionWise
 Select gl.location_region,
        SUM(wtd.Page_Views_Per_Session) as Total_Page_Views
 from Website_Traffic_Data wtd
 JOIN Geo_Lookup gl 
  ON  wtd.Location_Key = gl.Location_Key
  Group by gl.Location_Region
 
 
---Query 2 Total Session Duration by RegionWise
 Select gl.Location_Region,
        CONCAT(SUM(wtd.Session_Duration_Seconds) / 3600,'%')  as Total_Session_Duration
 from Website_Traffic_Data wtd
 JOIN Geo_Lookup gl 
  ON wtd.Location_Key =  gl.Location_Key
 Group by gl.Location_Region
 
 
---Query 4 Total Session Duration - Top 5 Cities
 With CTE
 AS
   (Select gl.location_city AS Cities,
           SUM(Session_Duration_Seconds) / 3600 AS Total_Session_Duration,
   DENSE_RANK() Over(Order by SUM(session_duration_seconds)/3600 desc) as RNK
    from Website_Traffic_Data wtd
JOIN Geo_Lookup gl ON wtd.Location_Key = gl.Location_Key
Group by gl.Location_City
)
 
 Select Cities,
        Total_Session_Duration 
 from CTE
 where RNK < 6
 
 
---Query 5 Total Page Views - Top 5 Cities
 With CTE1
 AS
 (
  Select gl.Location_City AS Cities,
         SUM(Page_Views_Per_Session) AS Total_Page_Views,
DENSE_RANK() Over(order by SUM(Page_Views_Per_Session) desc) AS RNK
  from Website_Traffic_Data wtd
  JOIN Geo_Lookup gl 
  on wtd.Location_Key = gl.Location_Key
  Group by gl.Location_City
  )
 
  Select Cities,
         Total_Page_Views
  from CTE1
  where RNK < 6
 
 
---Query 6 Total Number of Sessions - TOP 5 Cities
With CTE
AS
(
 Select gl.location_city AS Cities,
        COUNT(Session_Id) AS Total_Number_of_Sessions,
DENSE_RANK() Over(order by COUNT(session_id) desc) AS RNK
 from Website_Traffic_Data wtd
 JOIN Geo_Lookup gl ON wtd.Location_Key = gl.Location_Key
 Group by gl.Location_City
)
 
 Select Cities,
        Total_Number_of_Sessions
 from CTE
 where RNK < 6
 
 
 ---Query 7 Bounce Rate - TOP 5 Cities
 With CTE1
 AS
 (
   Select gl.Location_City AS Cities,
          COUNT(wtd.Session_Id) AS Total_Number_of_Sessions
   from Website_Traffic_Data wtd
   INNER JOIN Geo_Lookup gl  
                ON wtd.Location_Key = gl.Location_Key
   Group by gl.Location_City
 )
 
 
Select * from (
  Select *, DENSE_RANK() Over(Order by Bounce_Rate desc) AS RNK
   from
  (Select gl.location_city as cities,
         COUNT(CAST(wtd.Session_Id As decimal(10,2))) / Max(CAST(CTE1.Total_Number_of_Sessions AS decimal(10,2))) * 100 AS Bounce_Rate
 from Website_Traffic_Data wtd
 INNER JOIN Geo_Lookup gl
    ON wtd.Location_Key = gl.Location_Key
 INNER JOIN CTE1 
    ON CTE1.Cities = gl.Location_City
  where wtd.page_views_per_session < 2
 Group by gl.Location_City
 ) a ) b
 where b.RNK < 6
 
 
 
 ---Query 3 Bounce Rate by RegionWise
 With cte
 as
 (
    Select 
    gl.location_region AS Region,
COUNT(wtd.session_id) AS total_session
FROM
Website_Traffic_Data wtd
INNER JOIN Geo_Lookup gl 
ON wtd.Location_Key = gl.Location_Key
  Group by gl.Location_Region
 )
 
 
 Select gl.location_region,
        COUNT(CAST(wtd.Session_Id as decimal(10,2))) / MAX(CAST(cte.total_session AS decimal(10,2))) * 100 AS Bounce_Rate
 from Website_Traffic_Data wtd
 JOIN Geo_Lookup gl 
   ON wtd.Location_Key = gl.Location_Key
 JOIN cte
   ON cte.Region = gl.Location_Region
 where wtd.page_views_per_session < 2
 Group by gl.Location_Region
 
 
 -------- Dashboard Analysis ---------------------------------------
 -----Query 1 Average Session Duration(Hrs)
 Select AVG(CAST(wtd.Session_Duration_Seconds AS decimal(10,2))) /3600  AS Average_Session_Duration 
 from Website_Traffic_Data wtd
 
 
 ---Query 2 Total Page Views 
 Select SUM(wtd.Page_Views_Per_Session) as Total_Page_Views
 from Website_Traffic_Data wtd
 
 
 ---Query 3 Total Session Duration
 Select SUM(wtd.Session_Duration_Seconds)/3600 as Total_Session_Duration 
 from Website_Traffic_Data wtd
 
 
 ---Query 4 BounceRate
 Select 
     COUNT(CAST(wtd.Session_Id AS decimal(10,2))) / (Select
COUNT(CAST(wtd.Session_Id AS decimal(10,2))) as Total_Session
from
     Website_Traffic_Data wtd ) * 100 AS Bounce_Rate 
 from Website_Traffic_Data wtd
 where wtd.Page_Views_Per_Session < 2
 
 
 ---Query Total Session Duration Trend
 Select DATEPART(YEAR,Date_Key) AS YEAR,
        DATEPART(QUARTER,Date_Key) AS QUARTER,
SUM(Session_Duration_Seconds) / 3600 as Total_Session_Duration
 from Website_Traffic_Data
 Group by DATEPART(YEAR,Date_Key),
          DATEPART(QUARTER,Date_Key)
 Order by YEAR,QUARTER 
 
 
 ---Query Average Session Duration Trend
 Select DATEPART(YEAR,Date_Key) AS YEAR,
        DATEPART(QUARTER,Date_Key) AS QUARTER,
AVG(CAST(Session_Duration_Seconds AS decimal(10,2))) / 3600 as AVG_Session_Duration
 from Website_Traffic_Data
 Group by DATEPART(YEAR,Date_Key),
          DATEPART(QUARTER,Date_Key)
 Order by YEAR,QUARTER 
 
 
 ---Query BounceRate Trend
 With CTE
 AS
 (
   Select
        DATEPART(YEAR,date_key) as YEAR,
DATEPART(QUARTER,date_key) as QUARTER,
        COUNT(wtd.session_id) as Total_Session
   from Website_Traffic_Data wtd
   Group by DATEPART(YEAR,date_key),
DATEPART(QUARTER,date_key) 
 )
 
 Select DATEPART(YEAR,date_key) as YEAR,
DATEPART(QUARTER,date_key) as QUARTER,
COUNT(CAST(wtd.Session_Id As decimal(10,2))) / MAX(CAST(CTE.Total_Session AS decimal(10,2))) * 100 as Bounce_Rate
 from Website_Traffic_Data wtd
 JOIN CTE ON 
           CTE.YEAR =  DATEPART(YEAR,date_key) and CTE.QUARTER = DATEPART(QUARTER,date_key)
 where wtd.Page_Views_Per_Session < 2
Group by DATEPART(YEAR,date_key),
DATEPART(QUARTER,date_key)
 
 
---Query Total Page Session Trend
Select DATEPART(YEAR,Date_Key) as YEAR,
       DATEPART(QUARTER,Date_Key) as QUARTER,
   SUM(Page_Views_Per_Session) as Total_Page_Views
from Website_Traffic_Data wtd
Group by DATEPART(YEAR,Date_Key),
       DATEPART(QUARTER,Date_Key) 
Order by Total_Page_Views desc
 
 
--------------------------TRIGGER--------------------------------------
Alter trigger TG_DeviceInsert ON Device_Lookup
FOR INSERT
AS
BEGIN
   
Declare @DeviceName varchar(50);
 
Select @DeviceName = Device_Name from Inserted
 
Insert into Logs(Log_Message,LogDate)
Values(@DeviceName +' '+ 'has been entered successfully',GETDATE())
END