Creates marketing engagements on a marketing event

Engagements on marketing events represent customer activity taken on the marketing event before customers reach the shop’s website.

Script shopify Verified

by hugo697 · 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates marketing engagements on a marketing event
7
 * Engagements on marketing events represent customer activity taken on the marketing event before customers reach the shop’s website.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  marketing_event_id: string,
13
  body: {
14
    engagements?: {
15
      ad_spend?: number;
16
      clicks_count?: number;
17
      favorites_count?: number;
18
      is_cumulative?: boolean;
19
      occurred_on?: string;
20
      views_count?: number;
21
      [k: string]: unknown;
22
    }[];
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/marketing_events/${marketing_event_id}/engagements.json`
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      "X-Shopify-Access-Token": auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44