@@ -82,8 +82,8 @@ public function getPageInsightsMetricsData($pageId, $insightsMetrics, $since, $u
82
82
$ intervals = $ this ->getIntervalsForPeriod ($ since , $ until );
83
83
84
84
foreach ($ intervals as $ interval ) {
85
- $ params ["since " ] = $ interval [' since ' ];
86
- $ params ["until " ] = $ interval [' until ' ];
85
+ $ params ["since " ] = $ interval [" since " ];
86
+ $ params ["until " ] = $ interval [" until " ];
87
87
$ requests [] = $ this ->createRequest ("GET " , "/ {$ pageId }/insights " , $ params );
88
88
}
89
89
@@ -168,6 +168,56 @@ public function getPageInsightsForTotalValueMetrics($pageId, $metrics, $period,
168
168
return $ data ;
169
169
}
170
170
171
+ /*
172
+ * Get page insights for total value metrics in batch for multiple date ranges
173
+ */
174
+ public function getPageInsightsBatchTotalValueMetrics ($ pageId , $ metrics , $ period , $ days )
175
+ {
176
+ $ result = [];
177
+ $ metricsString = join (", " , $ metrics );
178
+ $ batchSize = 50 ; // Facebook's batch request limit
179
+
180
+ // Process days in chunks of 50
181
+ $ dayChunks = array_chunk ($ days , $ batchSize );
182
+
183
+ foreach ($ dayChunks as $ chunk ) {
184
+ $ batchRequests = [];
185
+
186
+ // Create a batch request for each date range in this chunk
187
+ foreach ($ chunk as $ day ) {
188
+ $ params = [
189
+ "metric " => $ metricsString ,
190
+ "metric_type " => "total_value " ,
191
+ "period " => $ period ,
192
+ "since " => $ day ["since " ],
193
+ "until " => $ day ["until " ],
194
+ ];
195
+
196
+ $ request = $ this ->createRequest ("GET " , "/ {$ pageId }/insights " , $ params );
197
+ $ batchRequests [$ day ["since " ]] = $ request ;
198
+ }
199
+
200
+ // Send this batch of requests
201
+ $ responses = $ this ->sendBatchRequest ($ batchRequests );
202
+ if (!empty ($ responses )) {
203
+ foreach ($ responses as $ timestamp => $ response ) {
204
+ $ decodedBody = $ response ->getDecodedBody ();
205
+
206
+ if (!empty ($ decodedBody ) && is_array ($ decodedBody )) {
207
+ $ responseData = $ decodedBody ["data " ];
208
+ $ data = [];
209
+ foreach ($ responseData as $ metric ) {
210
+ $ data [$ metric ["name " ]] = $ metric ["total_value " ]["value " ];
211
+ }
212
+ $ result [$ timestamp ] = $ data ;
213
+ }
214
+ }
215
+ }
216
+ }
217
+
218
+ return $ result ;
219
+ }
220
+
171
221
/*
172
222
* Get Instagram Graph node metadata
173
223
*/
0 commit comments