-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunnels_complete.php
223 lines (192 loc) · 7.29 KB
/
funnels_complete.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
include_once('ghl_api_conf.php');
include_once('functions.php');
//Test Sheet ID and Name
//$sheet_ID = 'XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXX';
//$sheet_name = 'Sheet1'; // Change this to the name of the sheet you want to target
//Production Sheet ID and Name
$sheet_ID = 'XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$sheet_name = 'GHL ALL Funnels and pages (PHP)'; // Change this to the name of the sheet you want to target
$gSheetService = new Google_Service_Sheets($g_client);
/* Make sure that the GSheet is shared with this user:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.iam.gserviceaccount.com
*/
// Clear the specific sheet before writing new values and remove all formatting
$sheetId = getSheetId($gSheetService, $sheet_ID, $sheet_name);
$requests = [
new Google_Service_Sheets_Request([
'updateCells' => [
'range' => [
'sheetId' => $sheetId,
],
'fields' => 'userEnteredValue'
]
]),
new Google_Service_Sheets_Request([
'repeatCell' => [
'range' => [
'sheetId' => $sheetId,
],
'cell' => [
'userEnteredFormat' => [
'backgroundColor' => [
'red' => 1,
'green' => 1,
'blue' => 1
]
]
],
'fields' => 'userEnteredFormat.backgroundColor'
]
])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
$gSheetService->spreadsheets->batchUpdate($sheet_ID, $batchUpdateRequest);
function print_funnels_table($funnels) {
global $locationID, $gSheetService, $sheet_ID, $sheet_name, $sheetId;
// Prepare values to write to the Google Sheet
$values = [
['Funnel Name', 'Funnel URL', 'Step Name', 'Step URL']
];
?>
<style>
table {font-family: tahoma; font-size:12px;}
#process-indicator {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background: rgba(0, 0, 0, 0.8);
color: white;
font-size: 16px;
border-radius: 5px;
z-index: 1000;
}
</style>
<?php
echo '<table border="1" cellspacing="0" cellpadding="10">';
echo '<tr><th>Funnel Name</th><th>Funnel URL</th><th>Step Name</th><th>Step URL</th></tr>';
$color = '#f2f2f2';
$rowCount = 1; // Start counting rows for conditional formatting
$rowRanges = [];
foreach ($funnels as $funnel) {
$step_count = count($funnel['steps']);
$first_row_printed = false;
$color = ($color == '#f2f2f2') ? '#ffffff' : '#f2f2f2'; // Alternate row color
$startRow = $rowCount;
if ($step_count > 0) {
foreach ($funnel['steps'] as $step) {
echo '<tr style="background-color:' . $color . ';">';
if (!$first_row_printed) {
$funnelName = $funnel['name'];
$funnelUrl = 'https://app.gohighlevel.com/v2/location/' . $locationID . '/funnels-websites/funnels/' . $funnel['_id'];
echo '<td>' . $funnelName . '</td>';
echo '<td><a href="' . $funnelUrl . '">' . $funnelUrl . '</a></td>';
$first_row_printed = true;
} else {
echo '<td></td><td></td>';
}
$stepName = $step['name'];
$stepUrl = 'https://your_domain' . $step['url'];
echo '<td>' . $stepName . '</td>';
echo '<td><a href="' . $stepUrl . '">' . $stepUrl . '</a></td>';
echo '</tr>';
// Add row to the Google Sheet values
$values[] = [$funnelName, $funnelUrl, $stepName, $stepUrl];
$rowCount++;
}
} else {
$funnelName = $funnel['name'];
$funnelUrl = 'https://app.gohighlevel.com/v2/location/' . $locationID . '/funnels-websites/funnels/' . $funnel['_id'];
echo '<tr style="background-color:' . $color . ';">';
echo '<td>' . $funnelName . '</td>';
echo '<td><a href="' . $funnelUrl . '">' . $funnelUrl . '</a></td>';
echo '<td>No steps available</td><td></td>';
echo '</tr>';
// Add row to the Google Sheet values
$values[] = [$funnelName, $funnelUrl, 'No steps available', ''];
$rowCount++;
}
$endRow = $rowCount;
$rowRanges[] = ['start' => $startRow, 'end' => $endRow, 'color' => $color];
}
echo '</table>';
// Write values to the Google Sheet
$body = new Google_Service_Sheets_ValueRange(['values' => $values]);
$options = ['valueInputOption' => 'USER_ENTERED'];
$result = $gSheetService->spreadsheets_values->update($sheet_ID, $sheet_name . '!A1', $body, $options);
// Output the result
print($result->getUpdatedRange() . PHP_EOL);
// Apply alternating row colors by funnel groups
$requests = [];
foreach ($rowRanges as $range) {
$requests[] = new Google_Service_Sheets_Request([
'repeatCell' => [
'range' => [
'sheetId' => $sheetId,
'startRowIndex' => $range['start'],
'endRowIndex' => $range['end'],
'startColumnIndex' => 0,
'endColumnIndex' => 4
],
'cell' => [
'userEnteredFormat' => [
'backgroundColor' => ($range['color'] == '#f2f2f2') ?
['red' => 0.95, 'green' => 0.95, 'blue' => 0.95] :
['red' => 1, 'green' => 1, 'blue' => 1]
]
],
'fields' => 'userEnteredFormat.backgroundColor'
]
]);
}
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
$gSheetService->spreadsheets->batchUpdate($sheet_ID, $batchUpdateRequest);
}
//log_error(json_encode($tokens));
if (isset($_SESSION['access_token'])) {
$access_token = $_SESSION['access_token'];
$queryParams = [
'locationId' => $locationID
];
$url = $funnels_url . '?' . http_build_query($queryParams);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer $access_token",
"Version: 2021-04-15"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
log_error("cURL Error #:" . $err);
} else {
$response_data = json_decode($response, true);
if (isset($response_data['funnels']) && is_array($response_data['funnels'])) {
print_funnels_table($response_data['funnels']);
} else {
echo 'No funnels found.';
}
}
} else {
echo 'Access token not available. Please authorize the app first.';
log_error('Access token not available. Please authorize the app first.');
}
?>