Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
1429 lines (1329 loc) · 43.5 KB
/
Copy pathapi.js
File metadata and controls
1429 lines (1329 loc) · 43.5 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fetch = require("node-fetch");
const crypto = require("crypto");
const GRAPH_HOST = "https://graph.facebook.com";
// The Graph/Marketing API version used when none is set in the plugin
// configuration. Meta releases a new version roughly every three months and
// keeps each one alive for about two years.
const DEFAULT_API_VERSION = "v26.0";
// Meta's Graph API only returns the object id unless you ask for more, so
// every read below has a sensible default set of fields.
const DEFAULT_FIELDS = {
adaccount: [
"id",
"account_id",
"name",
"account_status",
"currency",
"timezone_name",
"amount_spent",
"balance",
"spend_cap",
"business_name",
"created_time",
],
business: ["id", "name", "created_time", "verification_status"],
campaign: [
"id",
"account_id",
"name",
"status",
"effective_status",
"configured_status",
"objective",
"buying_type",
"bid_strategy",
"daily_budget",
"lifetime_budget",
"budget_remaining",
"start_time",
"stop_time",
"created_time",
"updated_time",
],
adset: [
"id",
"account_id",
"campaign_id",
"name",
"status",
"effective_status",
"configured_status",
"optimization_goal",
"billing_event",
"bid_amount",
"bid_strategy",
"daily_budget",
"lifetime_budget",
"budget_remaining",
"start_time",
"end_time",
"created_time",
"updated_time",
],
ad: [
"id",
"account_id",
"campaign_id",
"adset_id",
"name",
"status",
"effective_status",
"configured_status",
"bid_amount",
"preview_shareable_link",
"creative{id,name,object_type,thumbnail_url,image_url,video_id}",
"created_time",
"updated_time",
],
adcreative: [
"id",
"account_id",
"name",
"status",
"title",
"body",
"image_url",
"thumbnail_url",
"link_url",
"call_to_action_type",
"effective_object_story_id",
"object_story_spec",
"asset_feed_spec",
],
};
// Where the wording of an ad can be found: the headline, and the primary text
// that runs above the image. Meta puts both in a different place depending on
// how the ad was built, and nowhere at all on the creative when the ad
// promotes a post that already exists on the page.
const CREATIVE_TEXT_FIELDS = [
"id",
"name",
"title",
"body",
"object_story_spec",
"asset_feed_spec",
"effective_object_story_id",
];
// Where the picture or the film of an ad can be found. As with the wording,
// Meta keeps this in a different place depending on how the ad was built.
const CREATIVE_MEDIA_FIELDS = [
"id",
"name",
"account_id",
"object_type",
"image_url",
"image_hash",
"video_id",
"thumbnail_url",
"object_story_spec",
"asset_feed_spec",
"effective_object_story_id",
];
// Metrics that are valid at every insights level.
const BASE_INSIGHTS_FIELDS = [
"impressions",
"reach",
"frequency",
"clicks",
"ctr",
"cpc",
"cpm",
"spend",
"actions",
"action_values",
"date_start",
"date_stop",
];
// Naming the level adds the dimension columns that are valid at that level.
const INSIGHTS_LEVEL_FIELDS = {
account: ["account_id", "account_name"],
campaign: ["account_id", "account_name", "campaign_id", "campaign_name"],
adset: [
"account_id",
"account_name",
"campaign_id",
"campaign_name",
"adset_id",
"adset_name",
],
ad: [
"account_id",
"account_name",
"campaign_id",
"campaign_name",
"adset_id",
"adset_name",
"ad_id",
"ad_name",
],
};
// Insights columns that are returned as strings but are really numbers.
const NUMERIC_INSIGHTS_FIELDS = new Set([
"impressions",
"reach",
"frequency",
"clicks",
"unique_clicks",
"inline_link_clicks",
"ctr",
"unique_ctr",
"inline_link_click_ctr",
"cpc",
"cpm",
"cpp",
"cost_per_inline_link_click",
"spend",
"social_spend",
"objective_results",
]);
const insightsFields = (level) => [
...(INSIGHTS_LEVEL_FIELDS[level] || []),
...BASE_INSIGHTS_FIELDS,
];
/**
* Encode one value for the Graph API query string. Arrays of scalars become
* comma separated lists (fields, breakdowns); anything else structured -
* time_range, filtering - becomes JSON, which is what Meta expects.
*/
const queryValue = (v) => {
if (v === null || typeof v === "undefined") return null;
if (Array.isArray(v))
return v.every((e) => typeof e === "string" || typeof e === "number")
? v.join(",")
: JSON.stringify(v);
if (v instanceof Date) return v.toISOString();
if (typeof v === "object") return JSON.stringify(v);
if (typeof v === "boolean") return v ? "true" : "false";
return String(v);
};
const toQueryString = (q) =>
Object.entries(q || {})
.map(([k, v]) => [k, queryValue(v)])
.filter(([k, v]) => v !== null && v !== "")
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
/** Ad account ids are addressed as act_<id> in the Graph API */
const actId = (accountId) => {
const s = `${accountId}`.trim();
return s.startsWith("act_") ? s : `act_${s}`;
};
const appSecretProof = (access_token, app_secret) =>
crypto.createHmac("sha256", app_secret).update(access_token).digest("hex");
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Error codes worth trying again: transient failures and the various
// rate limit / business use case throttling codes.
const TRANSIENT_ERROR_CODES = new Set([1, 2, 4, 17, 32, 341, 613]);
const isTransientError = (error) => {
const code = +error?.code;
if (TRANSIENT_ERROR_CODES.has(code)) return true;
return code >= 80000 && code <= 80014;
};
const mkApiError = (error, status) => {
const bits = [`code ${error?.code}`];
if (error?.error_subcode) bits.push(`subcode ${error.error_subcode}`);
if (error?.fbtrace_id) bits.push(`fbtrace_id ${error.fbtrace_id}`);
const e = new Error(
`Meta Marketing API error (${bits.join(", ")}): ${
error?.error_user_msg || error?.message || "unknown error"
}`,
);
e.metaError = error;
e.status = status;
return e;
};
const apiUrl = (path, cfg) => {
if (/^https?:\/\//.test(path)) return path;
const version = cfg?.api_version || DEFAULT_API_VERSION;
return `${GRAPH_HOST}/${version}${path.startsWith("/") ? path : `/${path}`}`;
};
/**
* The single point through which every request to Meta goes.
*
* cfg: { access_token, app_id, app_secret, api_version, use_appsecret_proof,
* max_retries, log_requests }
* opts: { method, query, noAuth }
*/
const graphFetch = async (path, opts = {}, cfg = {}) => {
const { method = "GET", query, noAuth } = opts;
const url = apiUrl(path, cfg);
const params = { ...(query || {}) };
if (
!noAuth &&
cfg?.use_appsecret_proof &&
cfg?.app_secret &&
cfg?.access_token &&
!url.includes("appsecret_proof=")
)
params.appsecret_proof = appSecretProof(cfg.access_token, cfg.app_secret);
const qs = toQueryString(params);
const fullUrl = qs ? `${url}${url.includes("?") ? "&" : "?"}${qs}` : url;
const headers = { Accept: "application/json" };
if (!noAuth && cfg?.access_token)
headers.Authorization = `Bearer ${cfg.access_token}`;
const maxRetries = typeof cfg?.max_retries === "number" ? cfg.max_retries : 3;
let attempt = 0;
for (;;) {
if (cfg?.log_requests) console.log(`Meta ${method} ${fullUrl}`);
const response = await fetch(fullUrl, { method, headers });
const body = await response.text();
let json;
try {
json = JSON.parse(body);
} catch (e) {
if (response.status >= 500 && attempt < maxRetries) {
attempt += 1;
await sleep(1000 * Math.pow(2, attempt));
continue;
}
console.error(
`Meta Marketing API non-JSON response (HTTP ${response.status})`,
body,
);
throw new Error(
`Meta Marketing API: not a JSON response (HTTP ${response.status})`,
);
}
if (json && json.error) {
if (attempt < maxRetries && isTransientError(json.error)) {
attempt += 1;
await sleep(1000 * Math.pow(2, attempt));
continue;
}
throw mkApiError(json.error, response.status);
}
if (cfg?.log_requests) {
const usage = response.headers.get("x-business-use-case-usage");
if (usage) console.log("Meta business use case usage", usage);
}
return json;
}
};
/**
* Follow the cursor pagination on an edge and return the concatenated rows.
* Stops after max_pages pages so a misconfigured view cannot walk a whole
* account's history.
*/
const getAllPages = async (path, query, cfg = {}, opts = {}) => {
const maxPages = opts.max_pages || cfg?.max_pages || 10;
let json = await graphFetch(path, { query }, cfg);
const rows = [...(json?.data || [])];
let pages = 1;
while (json?.paging?.next && pages < maxPages) {
if (cfg?.page_delay)
await new Promise((resolve) => setTimeout(resolve, cfg?.page_delay));
json = await graphFetch(json.paging.next, {}, cfg);
rows.push(...(json?.data || []));
pages += 1;
}
return rows;
};
const withFields = (query, kind) => {
const q = { ...(query || {}) };
if (!q.fields && DEFAULT_FIELDS[kind]) q.fields = DEFAULT_FIELDS[kind];
return q;
};
const withInsightsFields = (query) => {
const q = { ...(query || {}) };
if (!q.fields) q.fields = insightsFields(q.level);
return q;
};
//
// Accounts and businesses
//
const getMe = async (query, cfg) =>
await graphFetch(
"/me",
{ query: { fields: "id,name", ...(query || {}) } },
cfg,
);
const getAdAccounts = async (query, cfg) =>
await getAllPages("/me/adaccounts", withFields(query, "adaccount"), cfg);
const getAdAccount = async (accountId, query, cfg) =>
await graphFetch(
`/${actId(accountId)}`,
{ query: withFields(query, "adaccount") },
cfg,
);
const getBusinesses = async (query, cfg) =>
await getAllPages("/me/businesses", withFields(query, "business"), cfg);
const getBusinessAdAccounts = async (businessId, query, cfg) =>
await getAllPages(
`/${businessId}/owned_ad_accounts`,
withFields(query, "adaccount"),
cfg,
);
//
// Campaigns, ad sets, ads and creatives
//
const getCampaigns = async (accountId, query, cfg) =>
await getAllPages(
`/${actId(accountId)}/campaigns`,
withFields(query, "campaign"),
cfg,
);
const getCampaign = async (campaignId, query, cfg) =>
await graphFetch(
`/${campaignId}`,
{ query: withFields(query, "campaign") },
cfg,
);
const getAdSets = async (accountId, query, cfg) =>
await getAllPages(
`/${actId(accountId)}/adsets`,
withFields(query, "adset"),
cfg,
);
const getCampaignAdSets = async (campaignId, query, cfg) =>
await getAllPages(`/${campaignId}/adsets`, withFields(query, "adset"), cfg);
const getAdSet = async (adSetId, query, cfg) =>
await graphFetch(`/${adSetId}`, { query: withFields(query, "adset") }, cfg);
const getAds = async (accountId, query, cfg) =>
await getAllPages(`/${actId(accountId)}/ads`, withFields(query, "ad"), cfg);
const getCampaignAds = async (campaignId, query, cfg) =>
await getAllPages(`/${campaignId}/ads`, withFields(query, "ad"), cfg);
const getAdSetAds = async (adSetId, query, cfg) =>
await getAllPages(`/${adSetId}/ads`, withFields(query, "ad"), cfg);
const getAd = async (adId, query, cfg) =>
await graphFetch(`/${adId}`, { query: withFields(query, "ad") }, cfg);
const getAdCreatives = async (accountId, query, cfg) =>
await getAllPages(
`/${actId(accountId)}/adcreatives`,
withFields(query, "adcreative"),
cfg,
);
const getAdCreative = async (creativeId, query, cfg) =>
await graphFetch(
`/${creativeId}`,
{ query: withFields(query, "adcreative") },
cfg,
);
/** Rendered HTML preview of an ad, as an iframe snippet */
const getAdPreview = async (adId, adFormat, cfg) => {
const json = await graphFetch(
`/${adId}/previews`,
{ query: { ad_format: adFormat || "DESKTOP_FEED_STANDARD" } },
cfg,
);
return json?.data?.[0]?.body || "";
};
/**
* Pull every headline out of a creative, best first. A carousel has one per
* card and a dynamic creative can carry several for Meta to choose between,
* so this returns a list rather than a single string.
*/
const creativeHeadlines = (creative) => {
const spec = creative?.object_story_spec || {};
const found = [
creative?.title,
spec.link_data?.name,
spec.video_data?.title,
spec.template_data?.name,
...(spec.link_data?.child_attachments || []).map((c) => c.name),
...(creative?.asset_feed_spec?.titles || []).map((t) => t.text),
];
return [...new Set(found.filter((h) => h))];
};
/**
* Pull every primary text out of a creative, best first. This is the longer
* wording that runs above the image, which Meta calls the primary text in Ads
* Manager and the body or the message in its API. As with headlines, a
* dynamic creative can carry several.
*/
const creativeBodies = (creative) => {
const spec = creative?.object_story_spec || {};
const found = [
creative?.body,
spec.link_data?.message,
spec.video_data?.message,
spec.photo_data?.caption,
spec.text_data?.message,
spec.template_data?.message,
...(creative?.asset_feed_spec?.bodies || []).map((b) => b.text),
];
return [...new Set(found.filter((b) => b))];
};
// A page post is only shown to a token that carries the page's own
// permissions. A token for the page can be asked for, once per page.
const PAGE_TOKEN_TTL_MS = 15 * 60 * 1000;
const pageTokenCache = new Map();
/**
* A token for one page, from the token in the settings. Comes back null when
* the settings token has no say over that page, which is the usual reason an
* ad that boosts a page post has nothing to show.
*/
const getPageAccessToken = async (pageId, cfg) => {
if (!pageId) return null;
const key = `${pageId}|${cfg?.access_token || ""}`;
const hit = pageTokenCache.get(key);
if (hit && Date.now() - hit.at < PAGE_TOKEN_TTL_MS) return hit.token;
let token = null;
try {
const json = await graphFetch(
`/${pageId}`,
{ query: { fields: "access_token" } },
cfg,
);
token = json?.access_token || null;
} catch (e) {
if (cfg?.log_requests)
console.log(`Meta: no token for page ${pageId}`, e.message);
}
pageTokenCache.set(key, { token, at: Date.now() });
return token;
};
/**
* Which tokens to try when reading the post behind an ad, best first: one
* for the page that owns it, then the one from the settings.
*/
const postTokens = async (creative, storyId, cfg, opts = {}) => {
const pageId =
creative?.object_story_spec?.page_id || `${storyId}`.split("_")[0];
const pageToken =
opts.page_token === false ? null : await getPageAccessToken(pageId, cfg);
return {
pageId,
pageToken,
cfgs: pageToken ? [{ ...cfg, access_token: pageToken }, cfg] : [cfg],
};
};
/**
* The wording of the page post an ad promotes: the post's own text, and the
* headline of the link it carries.
*/
const storyText = async (storyId, cfg) => {
const json = await graphFetch(
`/${storyId}`,
{ query: { fields: "message,attachments{title,description}" } },
cfg,
);
const attachment = json?.attachments?.data?.[0] || {};
return {
headline: attachment.title || "",
body: json?.message || attachment.description || "",
};
};
/**
* The wording of an ad: { headline, body }, where body is the primary text
* above the image. Takes an ad id, or an ad that has already been read with
* its creative, in which case nothing is read again.
*
* Ads that promote a post which already exists on the page keep their wording
* on the post, which needs a second read and a token with access to the page.
* When that read is not allowed the wording comes back empty rather than
* throwing.
*/
const getAdText = async (ad, cfg, opts = {}) => {
let creative = typeof ad === "object" ? ad?.creative : null;
if (!creative?.object_story_spec && !creative?.title && !creative?.body) {
const fetched = await graphFetch(
`/${typeof ad === "object" ? ad?.id : ad}`,
{ query: { fields: `creative{${CREATIVE_TEXT_FIELDS.join(",")}}` } },
cfg,
);
creative = fetched?.creative;
}
const text = {
headline: creativeHeadlines(creative)[0] || "",
body: creativeBodies(creative)[0] || "",
};
if ((!text.headline || !text.body) && creative?.effective_object_story_id) {
const storyId = creative.effective_object_story_id;
const { cfgs } = await postTokens(creative, storyId, cfg, opts);
for (const useCfg of cfgs)
try {
const story = await storyText(storyId, useCfg);
return {
headline: text.headline || story.headline,
body: text.body || story.body,
};
} catch (e) {
if (cfg?.log_requests)
console.log("Meta: could not read the post behind the ad", e.message);
}
}
return text;
};
/** The headline of an ad, as getAdText */
const getAdHeadline = async (ad, cfg) => (await getAdText(ad, cfg)).headline;
/** The primary text of an ad, the wording above the image, as getAdText */
const getAdBody = async (ad, cfg) => (await getAdText(ad, cfg)).body;
//
// Media: the picture or the film an ad is built on
//
// What tells two pieces of media apart. The same picture reached by two
// routes - once by hash and once by address - is the same picture.
const MEDIA_IDS = ["video_id", "image_hash", "url"];
/**
* Add one piece of media to the list, or, when it is already there under
* another of its names, fill in what that entry was missing.
*/
const addMedia = (out, m) => {
if (!m || !MEDIA_IDS.some((k) => m[k])) return;
const same = out.find(
(o) => o.kind === m.kind && MEDIA_IDS.some((k) => m[k] && o[k] === m[k]),
);
if (!same) {
out.push(m);
return;
}
Object.entries(m).forEach(([k, v]) => {
if (v && !same[k]) same[k] = v;
});
};
/** A carousel card, or a plain link ad, which are shaped the same way */
const linkMedia = (data) =>
data.video_id
? {
kind: "video",
video_id: data.video_id,
thumbnail_url: data.picture || data.image_url,
image_hash: undefined,
thumbnail_hash: data.image_hash,
name: data.name,
link: data.link,
}
: {
kind: "image",
url: data.picture || data.image_url,
image_hash: data.image_hash,
name: data.name,
link: data.link,
};
// Where an ad is mostly seen. A platform on its own means every position on it.
const MAIN_PLACEMENTS = ["facebook:feed", "instagram:stream", "facebook", "instagram"];
// The best rule priority of each piece of media on a creative that picks its
// media by placement, kept here rather than in what the caller gets back
const placementPriority = new WeakMap();
/**
* Where a placement rule shows its asset, as platform:position, or the
* platform alone when the rule takes every position on it. A rule that names
* nothing is the catch-all for whatever the other rules leave over.
*/
const rulePlacements = (spec = {}) => {
const platforms = spec.publisher_platforms?.length
? spec.publisher_platforms
: Object.keys(spec)
.filter((k) => k.endsWith("_positions"))
.map((k) => k.replace(/_positions$/, ""));
return platforms.flatMap((p) => {
const positions = spec[`${p}_positions`] || [];
return positions.length ? positions.map((pos) => `${p}:${pos}`) : [p];
});
};
/**
* Which placements the rules of a creative give one of its assets to, found
* through the labels the rules and the asset share. fallback says the asset
* is only there for the placements no other rule takes.
*/
const placementInfo = (asset, rules) => {
const labels = asset?.adlabels || [];
const used = rules.filter((r) =>
[r.image_label, r.video_label].some(
(l) => l && labels.some((a) => (l.id && a.id === l.id) || (l.name && a.name === l.name)),
),
);
if (!used.length) return {};
const perRule = used.map((r) => rulePlacements(r.customization_spec));
return {
placements: [...new Set(perRule.flat())],
fallback: perRule.every((p) => !p.length),
priority: Math.min(...used.map((r) => r.priority ?? Number.MAX_SAFE_INTEGER)),
};
};
const fileName = (url) => {
try {
return new URL(url).pathname.split("/").pop();
} catch (e) {
return "";
}
};
/**
* Put the media of a creative that picks its media by placement in order of
* how much of the ad it is: what the feed shows, then the picture the ad is
* known by, then what other placements show in the order Meta checks its
* rules, then the catch-all, then anything no rule uses. Every other creative
* keeps the order its media is shown in.
*/
const rankMedia = (media, creative) => {
if (!media.some((m) => m.placements)) return media;
const thumb = fileName(creative?.thumbnail_url);
const rank = (m) => {
if (!m.placements) return 4;
if (m.feed) return 0;
if (m.fallback) return 3;
if (thumb && [m.url, m.thumbnail_url].some((u) => u && fileName(u) === thumb))
return 1;
return 2;
};
const priority = (m) => placementPriority.get(m) ?? Number.MAX_SAFE_INTEGER;
return media.sort((a, b) => rank(a) - rank(b) || priority(a) - priority(b));
};
/**
* Every piece of media on a creative, without reading anything further. A
* video carries its own still as a thumbnail rather than as a picture of its
* own, so a film never counts as a picture too.
*/
const creativeMedia = (creative) => {
const spec = creative?.object_story_spec || {};
const feed = creative?.asset_feed_spec || {};
const out = [];
if (spec.video_data)
addMedia(out, {
kind: "video",
video_id: spec.video_data.video_id,
thumbnail_url: spec.video_data.image_url,
thumbnail_hash: spec.video_data.image_hash,
name: spec.video_data.title,
});
if (spec.photo_data)
addMedia(out, {
kind: "image",
url: spec.photo_data.url,
image_hash: spec.photo_data.image_hash,
});
[spec.link_data, spec.template_data].forEach((data) => {
if (!data) return;
const children = data.child_attachments || [];
if (children.length) children.forEach((c) => addMedia(out, linkMedia(c)));
else addMedia(out, linkMedia(data));
});
// Flexible and dynamic creatives keep a pool of assets for Meta to choose
// between, rather than one story. A creative customised by placement has
// rules saying which asset each placement shows.
const rules = feed.asset_customization_rules || [];
const addAsset = (asset, m) => {
if (rules.length) {
const { priority, ...info } = placementInfo(asset, rules);
Object.assign(m, info);
if (priority !== undefined) placementPriority.set(m, priority);
}
addMedia(out, m);
};
(feed.videos || []).forEach((v) =>
addAsset(v, {
kind: "video",
video_id: v.video_id,
thumbnail_url: v.thumbnail_url,
thumbnail_hash: v.thumbnail_hash,
}),
);
(feed.images || []).forEach((i) =>
addAsset(i, { kind: "image", url: i.url, image_hash: i.hash }),
);
if (rules.length) {
// The feed shows what a rule for the feed names, and when no rule names
// the feed, the catch-all
const inFeed = (m) => m.placements.some((p) => MAIN_PLACEMENTS.includes(p));
const feedRule = out.some((m) => m.placements && inFeed(m));
out.forEach((m) => {
if (m.placements) m.feed = feedRule ? inFeed(m) : !!m.fallback;
});
}
// Last, what the creative says about itself. On many ads this is the same
// media again, which the key above discards.
if (creative?.video_id)
addMedia(out, {
kind: "video",
video_id: creative.video_id,
thumbnail_url: creative.thumbnail_url,
});
else if (creative?.image_url || creative?.image_hash)
addMedia(out, {
kind: "image",
url: creative.image_url,
image_hash: creative.image_hash,
});
return rankMedia(out, creative);
};
/**
* The media on a page post an ad promotes, for ads that carry no creative of
* their own. An album post holds its pictures one level further down.
*/
const storyMedia = async (storyId, cfg) => {
const json = await graphFetch(
`/${storyId}`,
{
query: {
fields:
"permalink_url,full_picture," +
"attachments{media_type,media,target,url,subattachments{media_type,media,target}}",
},
},
cfg,
);
const out = [];
let album = false;
const fromAttachment = (att) => {
if (!att) return;
const type = `${att.media_type || ""}`.toLowerCase();
const still = att.media?.image?.src;
if (type.includes("video"))
addMedia(out, {
kind: "video",
video_id: att.target?.id,
thumbnail_url: still,
});
else if (still) addMedia(out, { kind: "image", url: still });
};
(json?.attachments?.data || []).forEach((att) => {
const subs = att.subattachments?.data || [];
if (subs.length > 1) album = true;
if (subs.length) subs.forEach(fromAttachment);
else fromAttachment(att);
});
// full_picture is whatever the post shows in a feed. For a film that is a
// still rather than the film, so it is offered as a thumbnail and never as
// a picture in its own right.
return {
media: out,
album,
thumbnail_url: json?.full_picture,
permalink_url: json?.permalink_url,
};
};
/**
* The media on the post an ad boosts, tried first with a token for the page
* that owns the post and then with the token from the settings. Never
* throws: what went wrong comes back as the reason, to be reported on the ad
* rather than stopping a run over a whole ad set.
*/
const postMedia = async (creative, cfg, opts = {}) => {
const storyId = creative?.effective_object_story_id;
if (!storyId) return { media: [] };
const { pageId, pageToken, cfgs } = await postTokens(
creative,
storyId,
cfg,
opts,
);
let reason;
for (const useCfg of cfgs) {
try {
const post = await storyMedia(storyId, useCfg);
if (post.media.length) return { ...post, story_id: storyId };
reason = `the post ${storyId} behind this ad carries no picture or film`;
} catch (e) {
reason = `could not read the post ${storyId} behind this ad: ${e.message}`;
if (cfg?.log_requests) console.log(`Meta: ${reason}`);
}
}
if (!pageToken)
reason = `${reason}. The access token has no say over page ${pageId}: to see what an ad that boosts a page post is made of, use a token that can also read that page, with the pages_read_engagement permission`;
return { media: [], story_id: storyId, error: reason };
};
/**
* Turn the ids and hashes collected above into addresses a file can be
* downloaded from. Videos are read one at a time, pictures all in one go.
*
* The address of a video is only given out to a token that owns it, and it is
* signed and short lived, so download it now rather than storing it. When it
* cannot be read the entry keeps its id and its thumbnail.
*/
const resolveMediaUrls = async (media, accountId, cfg, opts = {}) => {
const videos = media.filter((m) => m.kind === "video" && m.video_id);
const readVideo = (m, useCfg) =>
graphFetch(
`/${m.video_id}`,
{
query: {
fields: "id,source,picture,permalink_url,length,created_time",
},
},
useCfg,
);
// A film uploaded to the page rather than to the ad account is only given
// out to a token for that page, looked up once and only when needed
let pageCfg;
const forPage = async () => {
if (pageCfg === undefined) {
const token =
opts.pageId && opts.page_token !== false
? await getPageAccessToken(opts.pageId, cfg)
: null;
pageCfg = token ? { ...cfg, access_token: token } : null;
}
return pageCfg;
};
for (const m of videos) {
let v;
try {
v = await readVideo(m, cfg);
} catch (e) {
const useCfg = await forPage();
try {
if (!useCfg) throw e;
v = await readVideo(m, useCfg);
} catch (e2) {
m.error =
useCfg || !opts.pageId || opts.page_token === false
? e2.message
: `${e.message}. The film may belong to page ${opts.pageId}, which the access token has no say over: a token that can also read that page, with the pages_read_engagement permission, can download it`;
if (cfg?.log_requests)
console.log(`Meta: could not read video ${m.video_id}`, m.error);
continue;
}
}
m.url = v?.source || m.url;
m.thumbnail_url = m.thumbnail_url || v?.picture;
m.permalink_url = v?.permalink_url;
if (typeof v?.length === "number") m.length = v.length;
}
const needHash = media.filter((m) => !m.url && m.image_hash);
if (!needHash.length) return media;
if (!accountId) {
needHash.forEach((m) => {
m.error =
"the address of this picture is held by the ad account, which is not known here: pass the ad account id, or set a default one in the settings";
});
return media;
}
// The picture library only answers about so many at a time
const hashes = [...new Set(needHash.map((m) => m.image_hash))];
const byHash = {};
let reason;
for (let i = 0; i < hashes.length; i += 50) {
try {
const json = await graphFetch(
`/${actId(accountId)}/adimages`,
{
query: {
// a list of its own, not the comma separated kind Meta takes
// elsewhere
hashes: JSON.stringify(hashes.slice(i, i + 50)),
fields: "hash,url,permalink_url,width,height",
},
},
cfg,
);
// Depending on the API version this comes back as a list or as an
// object keyed by hash
const images = Array.isArray(json?.data)
? json.data
: Object.values(json?.images || {});
images.forEach((img) => {
if (img?.hash) byHash[img.hash] = img;
});
} catch (e) {
reason = `could not read the pictures of ad account ${actId(
accountId,
)}: ${e.message}`;
if (cfg?.log_requests) console.log(`Meta: ${reason}`);
}
}
needHash.forEach((m) => {
const img = byHash[m.image_hash];
if (!img) {
m.error =
reason ||
`the ad account has no picture with the hash ${m.image_hash}. It may belong to another ad account, or to the page rather than to the ad`;
return;
}
m.url = img.url;
m.permalink_url = img.permalink_url;
m.width = img.width;
m.height = img.height;
});