0

Create a Segment

by
Published Nov 29, 2022
Script mailchimp Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
/**
4
 * @param static_segment *(optional but either `static_segment` or `options` is required)*
5
 * An array of emails to be used for a static segment.
6
 * Any emails provided that are not present on the list will be ignored.
7
 * Passing an empty array will create a static segment without any subscribers.
8
 * This field cannot be provided with the options field.
9
 *
10
 * @param options *(optional but either `static_segment` or `options` is required)*
11
 * The conditions of the segment. Static and fuzzy segments don't have conditions.
12
 * Find more information about conditions at
13
 * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
14
 */
15
type Mailchimp = {
16
  api_key: string;
17
  server: string;
18
};
19
export async function main(
20
  auth: Mailchimp,
21
  list_id: string,
22
  name: string,
23
  static_segment?: string[],
24
  options?: {
25
    match: string;
26
    conditions: any[];
27
  },
28
) {
29
  const url = new URL(
30
    `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
31
  );
32
  const body = {
33
    name,
34
    static_segment,
35
    options,
36
  };
37

38
  const response = await fetch(url, {
39
    method: "POST",
40
    headers: {
41
      Authorization: `Bearer ${auth.api_key}`,
42
    },
43
    body: JSON.stringify(removeObjectEmptyFields(body)),
44
  });
45

46
  if (!response.ok) {
47
    throw Error(await response.text());
48
  }
49
  return await response.json();
50
}
51

52
function removeObjectEmptyFields(
53
  object?: Record<string, any>,
54
  removeEmptyArraysAndObjects = true,
55
  createNewObject = true,
56
) {
57
  if (!object || typeof object !== "object") return {}
58
  const obj = createNewObject ? { ...object } : object
59
  const emptyValues = [undefined, null, ""]
60
  for (const key in obj) {
61
    const value = obj[key]
62
    if (emptyValues.includes(value)) {
63
      delete obj[key]
64
    } else if (typeof value === "object") {
65
      if (Object.keys(value).length) {
66
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
67
      }
68
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
69
        delete obj[key]
70
      }
71
    }
72
  }
73
  return obj
74
}
75

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
    5
     * An array of emails to be used for a static segment.
    6
     * Any emails provided that are not present on the list will be ignored.
    7
     * Passing an empty array will create a static segment without any subscribers.
    8
     * This field cannot be provided with the options field.
    9
     *
    10
     * @param options *(optional but either `static_segment` or `options` is required)*
    11
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
    12
     * Find more information about conditions at
    13
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
    14
     */
    15
    type Mailchimp = {
    16
      api_key: string;
    17
      server: string;
    18
    };
    19
    export async function main(
    20
      auth: Mailchimp,
    21
      list_id: string,
    22
      name: string,
    23
      static_segment?: string[],
    24
      options?: {
    25
        match: string;
    26
        conditions: any[];
    27
      },
    28
    ) {
    29
      const url = new URL(
    30
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
    31
      );
    32
      const body = {
    33
        name,
    34
        static_segment,
    35
        options,
    36
      };
    37
    
    
    38
      const response = await fetch(url, {
    39
        method: "POST",
    40
        headers: {
    41
          Authorization: `Bearer ${auth.api_key}`,
    42
        },
    43
        body: JSON.stringify(removeObjectEmptyFields(body)),
    44
      });
    45
    
    
    46
      if (!response.ok) {
    47
        throw Error(await response.text());
    48
      }
    49
      return await response.json();
    50
    }
    51