DataJet
  • Welcome to DataJet
  • Scripts
    • Introduction
    • Scripts
      • Input
      • HTTP
      • Scheduled
      • Event
      • Shipping Rate
      • Email
    • Creating a custom script
  • Liquid
    • Introduction
    • Tags
      • exit
      • function
      • json
      • push
      • run
    • Filters
      • array_difference
      • array_equal
      • array_intersection
      • array_symmetric_difference
      • base64_decode
      • base64_encode
      • concat
      • content
      • email
      • encode_uri
      • file
        • multipart
        • url
        • content
      • find
      • find_exp
      • flat
      • flow
      • flow_v2
      • ftp
      • graphql
      • group_by
      • group_by_exp
      • hmac_sha256
      • http
      • in_groups_of
      • in_timezone
      • log
      • parse_json
      • parse_csv
      • parse_xml
      • pop
      • push
      • random
      • remove_prop
      • rest
      • run
      • sha1
      • sum
      • time_add
      • time_subtract
      • type
      • where_exp
  • Misc
    • Credits and usage
    • GitHub Integration
      • Track changes in a script's source code
      • Connect development stores with development branches
      • Use one codebase across multiple Shopify stores
    • Functions
    • Variables
    • Shopify Flow Integration
      • V1
      • V2
    • Processing user submitted forms
Powered by GitBook
On this page
  1. Misc

Functions

PreviousUse one codebase across multiple Shopify storesNextVariables

Last updated 7 months ago

Functions allow you to create short scripts that can be later reused in in any of your other scripts.

To create a function, select Add new function script button.

Function arguments are automatically injected into function code. At the end of the function you should return response using return tag. Example: Send metrics to Klaviyo using Klaviyo API. Here is the code of the function:

{% json body %}
{
  "data": {
    "type": "event",
    "attributes": {
      "properties": {
        "membership": {{payload.property | default: "" | json }}
      },
      "metric": {
        "data": {
          "type": "metric",
          "attributes": {
            "name": {{ payload.metric | default: "" | json }}
          }
        }
      },
      "profile": {
        "data": {
          "type": "profile", 
          "attributes": {
            "email": {{ payload.email | default: "" | json }}
          }
        }
      }
    }
  }
}
{% endjson %}

{% json request_options %}
{ 
  "url": "https://a.klaviyo.com/client/events/?company_id={{KLAVIYO_TOKEN}}",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Revision": "2024-05-15"
  },
  "body": {{ body | json }}
}
{% endjson %}
{% assign response = request_options | http %}
{% return response %}

Above code creates a payload and sends it as HTTP requestt to Klaviyo API. Variable payload is scoped to the function and created when function is called. Here is how you should call this function from any other script:

{% json fn_input %}
  {
    "payload": {
      "email": "support@code57.pl",
      "property": "GOLD",
      "metric": "MEMBERSHIP UPDATE"
    }
  }
{% endjson %}

{% json fn_result %}
  {% function "send_metric_to_klaviyo", fn_input %}
{% endjson %}

{{ fn_result | log }}

To invoke a function you use function tag followed by function handle and function input. After execution, fn_result contains result of HTTP call made in send_metric_to_klaviyo function.

There are few limitiations to keep in mind when using functions:

  • task variables are not available in functions (you can pass it as function input)

  • you can access global variables in functions

  • you can't invoce another function inside function