This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.php
186 lines (175 loc) · 6.62 KB
/
queries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<section id="queries">
<div class="title">
<h2>2. 検索クエリ</h2>
<?php
if ( empty($siteName) ) {
echo '<p>' . $clientName . '</p>';
} else {
echo '<p>' . $siteName . '(' . $clientName . ')</p>';
}
?>
</div>
<p>Google検索:上位1,000件のランディングページ(日別)</p>
<?php
echo '<p><time datetime="' . $startDate . '">' . $startDateDisplay . '</time> - <time datetime="' . $endDate . '">' . $endDateDisplay . '</time></p>';
?>
<?php
$queryChartCmd = "$pythonPath scChart.py $scKeyFileLocation $siteUrl $startDate $endDate";
$queryChartResponseRaw = shell_exec($queryChartCmd);
$queryChartResponse = json_decode($queryChartResponseRaw,true);
function queryChart($reports) {
$rows = $reports['rows'];
if ( $rows == !0 ) {
for ($index = 0; $index < count($rows); $index++) {
$dateRaw[$index] = $rows[$index]['keys'][0];
$date[$index] = date('n月j日',strtotime($dateRaw[$index]));
$clicks[$index] = $rows[$index]['clicks'];
}
$dateJson = json_encode($date);
$clicksJson = json_encode($clicks);
echo <<<EOF
<section class="mainChart">
<canvas id="queryChart"></canvas>
<script>
var ctx = document.getElementById("queryChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: $dateJson,
datasets: [
{
label: 'クリック数',
data: $clicksJson,
borderColor: "#068dc7",
backgroundColor: "#068dc722",
pointBorderColor: "#068dc7",
pointBackgroundColor: "#068dc7"
}
]
},
options: {
maintainAspectRatio: false,
legend: {
align: 'start'
}
}
});
</script>
</section>
EOF;
}
}
queryChart($queryChartResponse);
?>
<?php
function queryResults($reports) {
$rows = $reports['rows'];
if ( $rows == !0 ) {
for ($index = 0; $index < count($rows); $index++) {
$totalClicksArray[$index] = $rows[$index]['clicks'];
$totalImpressionsArray[$index] = $rows[$index]['impressions'];
$totalCtrArray[$index] = $rows[$index]['ctr'];
$totalPositionArray[$index] = $rows[$index]['position'];
}
$totalClicksRaw = array_sum($totalClicksArray);
$totalImpressionsRaw = array_sum($totalImpressionsArray);
$totalCtrRaw = array_sum($totalCtrArray) / count($rows);
$totalPositionRaw = array_sum($totalPositionArray) / count($rows);
$totalClicks = number_format($totalClicksRaw);
$totalImpressions = number_format($totalImpressionsRaw);
$totalCtr = round($totalCtrRaw, 2);
$totalPosition = round($totalPositionRaw, 2);
if ( $totalClicksRaw == 0 ) {
$totalClicks_percentage = 0;
} else {
$totalClicks_percentage = round($totalClicksRaw / $totalClicksRaw * 100, 2);
}
if ( $totalImpressionsRaw == 0 ) {
$totalImpressions_percentage = 0;
} else {
$totalImpressions_percentage = round($totalImpressionsRaw / $totalImpressionsRaw * 100, 2);
}
if ( $totalCtrRaw == 0 ) {
$totalCtr_avgPercentage = 0;
} else {
$totalCtr_avgPercentage = round($totalCtrRaw / $totalCtrRaw * 100 - 100, 2);
}
if ( $totalPositionRaw == 0 ) {
$totalPosition_avgPercentage = 0;
} else {
$totalPosition_avgPercentage = round($totalPositionRaw / $totalPositionRaw * 100 - 100, 2);
}
} else {
$totalClicks = 0;
$totalClicks_percentage = 0;
$totalImpressions = 0;
$totalImpressions_percentage = 0;
$totalCtr = 0;
$totalCtr_avgPercentage = 0;
$totalPosition = 0;
$totalPosition_avgPercentage = 0;
}
echo <<<EOF
<table>
<colgroup>
<col span="2">
<col class="active">
<col span="3">
</colgroup>
<thead>
<tr>
<th colspan="2">検索クエリ</th>
<th>クリック数</th>
<th>表示回数</th>
<th>クリック率</th>
<th>平均掲載順位</th>
</tr>
<tr>
<td colspan="2"></td>
<td>$totalClicks<br><span class="small">全体に<wbr>対する<wbr>割合:<br>{$totalClicks_percentage}%<br>($totalClicks)</span></td>
<td>$totalImpressions<br><span class="small">全体に<wbr>対する<wbr>割合:<br>{$totalImpressions_percentage}%<br>($totalImpressions)</span></td>
<td>{$totalCtr}%<br><span class="small">ビューの<wbr>平均:<br>{$totalCtr}%<br>({$totalCtr_avgPercentage}%)</span></td>
<td>$totalPosition<br><span class="small">ビューの<wbr>平均:<br>$totalPosition<br>({$totalPosition_avgPercentage}%)</span></td>
</tr>
</thead>
<tbody>
EOF;
if ( $rows == !0 ) {
for ($index = 0; $index < min( count($rows), 25); $index++) {
$rank = $index + 1;
$title = $rows[$index]['keys'][0];
$clicksRaw = $rows[$index]['clicks'];
$impressionsRaw = $rows[$index]['impressions'];
$ctrRaw = $rows[$index]['ctr'];
$positionRaw = $rows[$index]['position'];
$clicks = number_format($clicksRaw);
$impressions = number_format($impressionsRaw);
$ctr = round($ctrRaw, 2);
$position = round($positionRaw, 2);
if ( $totalClicksRaw == 0 ) {
$clicks_percentage = 0;
} else {
$clicks_percentage = round($clicksRaw / $totalClicksRaw * 100, 2);
}
if ( $totalImpressionsRaw == 0 ) {
$impressions_percentage = 0;
} else {
$impressions_percentage = round($impressionsRaw / $totalImpressionsRaw * 100, 2);
}
echo <<<EOF
<tr>
<td class="rank">$rank</td>
<td class="title-col">$title</td>
<td>$clicks<br><span class="small">({$clicks_percentage}%)</span></td>
<td>$impressions<br><span class="small">({$impressions_percentage}%)</span></td>
<td>{$ctr}%</td>
<td>$position</td>
</tr>
EOF;
}
}
echo '</tbody></table>';
}
queryResults($queryResponse);
?>
</section>