Created
November 4, 2021 19:17
-
-
Save stevygee/30b32eb1edd6e255a08eaa9fc6cdee9d to your computer and use it in GitHub Desktop.
x-fran/g-trends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Google\GTrends; | |
# This options are by default if none provided | |
$options = [ | |
'hl' => 'en-US', | |
'tz' => -60, # last hour | |
'geo' => 'IE', | |
]; | |
$gt = new GTrends($options); | |
$keyword = 'Dublin'; | |
$response = $gt->interestOverTime($keyword, 0, 'today 12-m'); | |
$js_data = ''; | |
foreach( $response as $datapoint ) { | |
$js_data .= '[new Date("' . $datapoint[formattedAxisTime] . '"), ' . $datapoint[value][0] . '], '; | |
} | |
?> | |
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> | |
<div id="chart_div"></div> | |
<p>Source: Google Trends</p> | |
<?php | |
echo '<pre>'; | |
print_r($response); # one year | |
echo '</pre>'; | |
?> | |
<script> | |
google.charts.load('current', {packages: ['corechart', 'line']}); | |
google.charts.setOnLoadCallback(drawBasic); | |
function drawBasic() { | |
var data = new google.visualization.DataTable(); | |
data.addColumn('date', 'X'); | |
data.addColumn('number', '<?php echo $keyword; ?>'); | |
data.addRows([ | |
<?php echo $js_data; ?> | |
]); | |
var options = { | |
hAxis: { | |
title: 'Time' | |
}, | |
vAxis: { | |
title: 'Popularity' | |
} | |
}; | |
var chart = new google.visualization.LineChart(document.getElementById('chart_div')); | |
chart.draw(data, options); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment