0

Send conversions

by
Published Dec 20, 2024

The Pinterest API offers advertisers a way to send Pinterest their conversion information (including web conversions, in-app conversions, or even offline conversions) based on their ad_account_id.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Send conversions
7
 * The Pinterest API offers advertisers a way to send Pinterest their conversion information (including web conversions, in-app conversions, or even offline conversions) based on their ad_account_id.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  test: string | undefined,
13
  body: {
14
    data: {
15
      event_name: string;
16
      action_source: string;
17
      event_time: number;
18
      event_id: string;
19
      event_source_url?: string;
20
      opt_out?: false | true;
21
      partner_name?: string;
22
      user_data:
23
        | {
24
            em: string[];
25
            hashed_maids?: string[];
26
            client_ip_address?: string;
27
            client_user_agent?: string;
28
          }
29
        | {
30
            em?: string[];
31
            hashed_maids: string[];
32
            client_ip_address?: string;
33
            client_user_agent?: string;
34
          }
35
        | {
36
            em?: string[];
37
            hashed_maids?: string[];
38
            client_ip_address: string;
39
            client_user_agent: string;
40
          };
41
      custom_data?: {
42
        currency?: string;
43
        value?: string;
44
        content_ids?: string[];
45
        content_name?: string;
46
        content_category?: string;
47
        content_brand?: string;
48
        contents?: {
49
          id?: string;
50
          item_price?: string;
51
          quantity?: number;
52
          item_name?: string;
53
          item_category?: string;
54
          item_brand?: string;
55
        }[];
56
        num_items?: number;
57
        order_id?: string;
58
        search_string?: string;
59
        opt_out_type?: string;
60
        np?: string;
61
      };
62
      app_id?: string;
63
      app_name?: string;
64
      app_version?: string;
65
      device_brand?: string;
66
      device_carrier?: string;
67
      device_model?: string;
68
      device_type?: string;
69
      os_version?: string;
70
      wifi?: false | true;
71
      language?: string;
72
    }[];
73
  },
74
) {
75
  const url = new URL(
76
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/events`,
77
  );
78
  for (const [k, v] of [["test", test]]) {
79
    if (v !== undefined && v !== "" && k !== undefined) {
80
      url.searchParams.append(k, v);
81
    }
82
  }
83
  const response = await fetch(url, {
84
    method: "POST",
85
    headers: {
86
      "Content-Type": "application/json",
87
      Authorization: "Bearer " + auth.token,
88
    },
89
    body: JSON.stringify(body),
90
  });
91
  if (!response.ok) {
92
    const text = await response.text();
93
    throw new Error(`${response.status} ${text}`);
94
  }
95
  return await response.json();
96
}
97