In [26]:
#Author: Rijal Fahmi Mohamadi ( bloggerpi.com )
#install pytrends first if not installed, read https://pypi.org/project/pytrends/ for more documentation
#pip install pytrends
#This dashboard can be used to monitor the results of your branding efforts across all your marketing channels.
#Additionally, if you are undertaking a rebranding initiative, it can help you determine the success of your rebranding efforts.
#For example, it can track the transition from 'Jurnal by Mekari' to 'Mekari Jurnal'.
#This Python script is used for trend analysis based on Google Trends' public data across various time frames.
#I created this script based on this tutorial (https://www.scraperapi.com/blog/google-trends-scraper/) and modified it according to my requirements.
#for debugging and knowledge base related i use this resource from the internet:
#1. https://stackoverflow.com/questions/22070263/create-a-legend-with-pandas-and-matplotlib-pyplot
#2. https://www.freecodecamp.org/news/matplotlib-figure-size-change-plot-size-in-python/
#3. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
#4. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html
#5. https://www.statology.org/pandas-plot-legend/
In [27]:
from pytrends.request import TrendReq #For usage read https://pypi.org/project/pytrends/ for more documentation
import matplotlib.pyplot as plt
In [21]:
#set language and region
pytrends = TrendReq(hl='en-ID')
#payload parameter
keywords = ['mekari jurnal', 'accurate online', 'hashmicro', 'majoo', 'jurnal by mekari'] #change this value to keywords that you want to check the trends
timeframes = ["today 5-y", "today 12-m", "today 3-m", "today 1-m"] #time frame: last 5 years, last 12 monts, last 3 months, last 1 month
#Build payload / request parameter example
#pytrends.build_payload(keywords,timeframe=timeframes[0]) #Build payload with basic parameter
#pytrends.build_payload(keywords, cat=0, timeframe=timeframes[0], geo='ID', gprop='') #Build payload with complete parameter
In [22]:
print("Plotting Google Trends data using time frame: "+timeframes[3]+" or last 1 Month time frame")
pytrends.build_payload(keywords, cat=0, timeframe=timeframes[3], geo='ID', gprop='') #Build payload with complete parameter
trend_data = pytrends.interest_over_time() #return as dataframe
series = trend_data[keywords]
plt.figure(figsize=(28,10)) #set plot size
plt.plot(series)
plt.legend(keywords, loc='best')
plt.show()
for keyword in keywords:
#Calculate mean for each keywords
mean = round(trend_data.mean(), 2)
avg = round(trend_data[keyword][-52:].mean(),2)
trend = round(((avg/mean[keyword])-1)*100,2)
print("----------------------------------------")
print("Keyword Analysis For:" + keyword)
print("----------------------------------------")
print('The average last 1 months interest of ' + keyword + ' was ' + str(mean[keyword]))
print('')
Plotting Google Trends data using time frame: today 1-m or last 1 Month time frame
---------------------------------------- Keyword Analysis For:mekari jurnal ---------------------------------------- The average last 1 months interest of mekari jurnal was 6.11 ---------------------------------------- Keyword Analysis For:accurate online ---------------------------------------- The average last 1 months interest of accurate online was 52.82 ---------------------------------------- Keyword Analysis For:hashmicro ---------------------------------------- The average last 1 months interest of hashmicro was 4.75 ---------------------------------------- Keyword Analysis For:majoo ---------------------------------------- The average last 1 months interest of majoo was 64.61 ---------------------------------------- Keyword Analysis For:jurnal by mekari ---------------------------------------- The average last 1 months interest of jurnal by mekari was 1.43
In [23]:
print("Plotting Google Trends data using time frame: "+timeframes[2]+" or last 3 Month time frame")
pytrends.build_payload(keywords, cat=0, timeframe=timeframes[2], geo='ID', gprop='') #Build payload with complete parameter
trend_data = pytrends.interest_over_time() #return as dataframe
series = trend_data[keywords]
plt.figure(figsize=(28,10)) #set plot size
plt.plot(series)
plt.legend(keywords, loc='best')
plt.show()
for keyword in keywords:
#Calculate mean for each keywords
mean = round(trend_data.mean(), 2)
avg = round(trend_data[keyword][-52:].mean(),2)
trend = round(((avg/mean[keyword])-1)*100,2)
print("----------------------------------------")
print("Keyword Analysis For:" + keyword)
print("----------------------------------------")
print('The average last 3 months interest of ' + keyword + ' was ' + str(mean[keyword]))
print('')
Plotting Google Trends data using time frame: today 3-m or last 3 Month time frame
---------------------------------------- Keyword Analysis For:mekari jurnal ---------------------------------------- The average last 3 months interest of mekari jurnal was 5.89 ---------------------------------------- Keyword Analysis For:accurate online ---------------------------------------- The average last 3 months interest of accurate online was 46.03 ---------------------------------------- Keyword Analysis For:hashmicro ---------------------------------------- The average last 3 months interest of hashmicro was 4.71 ---------------------------------------- Keyword Analysis For:majoo ---------------------------------------- The average last 3 months interest of majoo was 66.37 ---------------------------------------- Keyword Analysis For:jurnal by mekari ---------------------------------------- The average last 3 months interest of jurnal by mekari was 1.39
In [24]:
print("Plotting Google Trends data using time frame: "+timeframes[1]+" or last 12 Months time frame")
pytrends.build_payload(keywords, cat=0, timeframe=timeframes[1], geo='ID', gprop='') #Build payload with complete parameter
trend_data = pytrends.interest_over_time() #return as dataframe
series = trend_data[keywords]
plt.figure(figsize=(25,10)) #set plot size
plt.plot(series)
plt.legend(keywords, loc='best')
plt.show()
for keyword in keywords:
#Calculate mean for each keywords
mean = round(trend_data.mean(), 2)
avg = round(trend_data[keyword][-52:].mean(),2)
trend = round(((avg/mean[keyword])-1)*100,2)
print("----------------------------------------")
print("Keyword Analysis For:" + keyword)
print("----------------------------------------")
print('The average last 12 months interest of ' + keyword + ' was ' + str(mean[keyword]))
print('')
Plotting Google Trends data using time frame: today 12-m or last 12 Months time frame
---------------------------------------- Keyword Analysis For:mekari jurnal ---------------------------------------- The average last 12 months interest of mekari jurnal was 6.79 ---------------------------------------- Keyword Analysis For:accurate online ---------------------------------------- The average last 12 months interest of accurate online was 48.88 ---------------------------------------- Keyword Analysis For:hashmicro ---------------------------------------- The average last 12 months interest of hashmicro was 4.81 ---------------------------------------- Keyword Analysis For:majoo ---------------------------------------- The average last 12 months interest of majoo was 73.69 ---------------------------------------- Keyword Analysis For:jurnal by mekari ---------------------------------------- The average last 12 months interest of jurnal by mekari was 0.44
In [25]:
print("Plotting Google Trends data using time frame: "+timeframes[0]+" or last 5 Years time frame")
pytrends.build_payload(keywords, cat=0, timeframe=timeframes[0], geo='ID', gprop='') #Build payload with complete parameter
trend_data = pytrends.interest_over_time() #return as dataframe
series = trend_data[keywords]
plt.figure(figsize=(25,10)) #set plot size
plt.plot(series)
plt.legend(keywords, loc='best')
plt.show()
def doAnalysis(keyword,trend):
#Stable trend
if mean[keyword] > 75 and abs(trend) <= 5:
print('The interest for ' + keyword + ' is stable in the last 5 years.')
elif mean[keyword] > 75 and trend > 5:
print('The interest for ' + keyword + ' is stable and increasing in the last 5 years.')
elif mean[keyword] > 75 and trend < -5:
print('The interest for ' + keyword + ' is stable and decreasing in the last 5 years.')
#Relatively stable
elif mean[keyword] > 60 and abs(trend) <= 15:
print('The interest for ' + keyword + ' is relatively stable in the last 5 years.')
elif mean[keyword] > 60 and trend > 15:
print('The interest for ' + keyword + ' is relatively stable and increasing in the last 5 years.')
elif mean[keyword] > 60 and trend < -15:
print('The interest for ' + keyword + ' is relatively stable and decreasing in the last 5 years.')
#Seasonal
elif mean[keyword] > 20 and abs(trend) <= 15:
print('The interest for ' + keyword + ' is seasonal.')
#New keyword
elif mean[keyword] > 20 and trend > 15:
print('The interest for ' + keyword + ' is trending.')
#Declining keyword
elif mean[keyword] > 20 and trend < -15:
print('The interest for ' + keyword + ' is significantly decreasing.')
#Cyclinal
elif mean[keyword] > 5 and abs(trend) <= 15:
print('The interest for ' + keyword + ' is cyclical.')
#New
elif mean[keyword] > 0 and trend > 15:
print('The interest for ' + keyword + ' is new and trending.')
#Declining
elif mean[keyword] > 0 and trend < -15:
print('The interest for ' + keyword + ' is declining and not comparable to its peak.')
#Other
else:
print('This is something to be checked.')
print('')
print("--------------------")
print("The Analysis For 5 Years Time Frame Data")
print("--------------------")
print('')
for keyword in keywords:
#Calculate mean for each keywords
mean = round(trend_data.mean(), 2)
avg = round(trend_data[keyword][-52:].mean(),2)
trend = round(((avg/mean[keyword])-1)*100,2)
print("----------------------------------------")
print("Keyword Analysis For:" + keyword)
print("----------------------------------------")
doAnalysis(keyword,trend)
print('The average 5 years interest of ' + keyword + ' was ' + str(mean[keyword]))
print('The last year interest of ' + keyword + ' compared to the last 5 years has changed by ' + str(trend) + '%.')
print('')
Plotting Google Trends data using time frame: today 5-y or last 5 Years time frame
-------------------- The Analysis For 5 Years Time Frame Data -------------------- ---------------------------------------- Keyword Analysis For:mekari jurnal ---------------------------------------- The interest for mekari jurnal is new and trending. The average 5 years interest of mekari jurnal was 3.1 The last year interest of mekari jurnal compared to the last 5 years has changed by 119.03%. ---------------------------------------- Keyword Analysis For:accurate online ---------------------------------------- The interest for accurate online is trending. The average 5 years interest of accurate online was 23.56 The last year interest of accurate online compared to the last 5 years has changed by 107.47%. ---------------------------------------- Keyword Analysis For:hashmicro ---------------------------------------- The interest for hashmicro is new and trending. The average 5 years interest of hashmicro was 2.36 The last year interest of hashmicro compared to the last 5 years has changed by 103.81%. ---------------------------------------- Keyword Analysis For:majoo ---------------------------------------- The interest for majoo is trending. The average 5 years interest of majoo was 20.62 The last year interest of majoo compared to the last 5 years has changed by 257.37%. ---------------------------------------- Keyword Analysis For:jurnal by mekari ---------------------------------------- The interest for jurnal by mekari is declining and not comparable to its peak. The average 5 years interest of jurnal by mekari was 0.62 The last year interest of jurnal by mekari compared to the last 5 years has changed by -29.03%.
In [ ]: