Search code examples
stackexchange-api

How can I find the number of views all MATLAB questions have received?


I want to calculate the total number of question views that a tag on Stack Overflow has. Let's say the tag is MATLAB. At present there are 88k questions asked on Stack Overflow with tagged . Now each of these questions has some views.

Is there a way for me to know the total question views that these 88k questions have together via Stack Exchange API?


Solution

  • There are quite a few questions and given that the API returns a maximum of 100 results per page and it has a limit of 10k calls per day (when you use a key), I wouldn't suggest using it in this case.

    Instead, use the Stack Exchange Data Explorer (SEDE). Here is an example SQL query which should satisfy your rrequirement:

    DECLARE @from_date AS date = '##FromDate##'
    DECLARE @to_date AS date = '##ToDate##'
    
    SELECT SUM(CAST(ViewCount AS BIGINT)) AS view_count FROM Posts
    WHERE PostTypeId = 1
      AND CreationDate > @from_date
      AND CreationDate < @to_date
      AND Tags LIKE '%##Tag##%'
    

    This will sum the the ViewCount columnn's values, which are the number of views each question has. Since you want only questions, you need to have PostTypeId set to 1.

    Note that this finds the total views of questions tagged . It's impossible to know the views a question has received up to a certain date.

    Here is the query live. Enter a tag name in the Tag input and Run Query! If the tag has many questions, then the query might time out.

    See also: Database schema documentation for the public data dump and SEDE on Meta Stack Exchange.