How to Automate Forecasting Organic Traffic Using Python

Photo of author
Written By Ed Roberts

A so-called SEO expert. 

A quick intro to forecasting organic traffic

Forecasting organic traffic is the bane of any SEO, and for the most part, is a losing battle. With SEO there are so many unknowns (algorithm updates, competitor strategies, changes to SERPs etc) that any real degree of accuracy is impossible (unless you get very lucky!). That doesn’t mean you won’t be asked to do it though!

Now you could down the route of devising a complex methodology for trying to forecast traffic based on keyword data or the perceived impact of ongoing work and upcoming projects, however, that will likely be a large amount of effort for something likely to be a ballpark figure anyway. Instead, save some time and get back to actually getting results by automating your forecasting with Python.

For this, we’re going to be leveraging Facebook’s open-source Prophet algorithm which is designed to automate forecasting time series data. It’s quick and easy to use, only really requiring you to source and format the data you want to forecast. For this example, I have taken organic sessions over 16-month period from GA4. To do this just go to the session report, filter by organic search and make sure to select the date as a secondary dimension. You can also easily forecast click data from Google Search Console using the Search Analytics for Sheets extension. You can then export this as a CSV. As with my other articles, you can copy the colab file here. Now let’s break down the script.

Install the necessary packages

#first we'll install the necessary packages

!pip install prophet
from prophet import Prophet
import pandas as pd

First off we’ll only need two packages for this script, Prophet and pandas. Make sure to remove the ! from the pip install if you’re not running it in Google Colab.

Import and prep the GA4 data

#time to import and format our session data to use as the basis of of our forecast

df = pd.read_csv('/content/GA4-data.csv')

df = df[['Date','Sessions']]

df.columns = ['ds', 'y']

df['ds'] = pd.to_datetime(df['ds'], format='%Y%m%d')

Now we’re going to create a data frame from the CSV file we downloaded. It’s important that we only select the data we’re interested in (sessions in this case) and the date. These need to be named ‘ds’ which is the datestamp and ‘y’ which is the metric we’ll be forecasting. It’s also crucial we format the datetime to one recognised by pandas which is why we’ve formatted is using the to_datetime() method. Now our data is ready for forecasting.

Fit the model and set the forecast

#now to fit the model

m = Prophet(yearly_seasonality=True, weekly_seasonality=True)
m.fit(df)

#lets set the number of days we want to forecast into the future

future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)

Now we’re going to fit the model, adding that there’s both yearly and weekly seasonality. We’re then going to specify the number of days into the future we want to forecast. This will leave us with a data frame that contains the predicted value under the ‘yhat’ column as well as columns for components and uncertainty intervals.

Visualise the forecast

Of course, to really understand and communicate any sort of forecast to other stakeholders proper visualisation is key. Fortunately, prophet comes with some excellent options for plotting data.

#lets visualise the forecast

from prophet.plot import plot_plotly, plot_components_plotly

plot_plotly(m, forecast)

You’ll end up with an interactive plot that looks something like this.

Plot showing a forecast of organic sessions over the next year.

You can also visualise the yearly and weekly seasonal trends with this plot.

#and the trend

plot_components_plotly(m, forecast)

And there we go, a very quick and efficient way to forecast organic traffic which will be about on par (or even more accurate) than any other method in a fraction of the time. Of course, another option is to just slap a 10% uplift on it and call it a day 😂. Want to learn more about how you can automate your SEO processes? Read my guide on automating alt text with AI and Python.