APIs and libraries for functions

This article provides information on the APIs and libraries available for functions.

Functions can use the following APIs and libraries:

  • Crypto-ES.js
  • TweetNaCl.js
  • JavaScript Console API

Crypto-ES.js

Crypto-ES.js is a library of encryption algorithms available that provides hashing, encoding, and cipher algorithms. For more information, see Crypto-ES.

The following example shows how to encrypt and decrypt text:

import CryptoES from 'crypto-es';

var encrypted = CryptoES.AES.encrypt("Message", "Secret Passphrase");
console.log(encrypted);
var decrypted = CryptoES.AES.decrypt(encrypted, "Secret Passphrase");
console.log(CryptoES.enc.Utf8.stringify(decrypted));

TweetNaCl.js

TweetNaCl is a crypto library that implements secret-key authenticated encryption, public-key authenticated encryption, and hashing and public-key signatures. The TweetNaCl library is available only for event and visitor functions.

Import the TweetNaCl.js module into your event or visitor function as follows:

import nacl from 'tweetnacl';

For more information, see NPM TweetNaCl.js.

JavaScript console API

Use the JavaScript console functions to write messages and errors to the log.

console.log() , console.info(), and console.debug() log messages go to the info output stream. console.warn() and console.error() log messages go to the error output stream.

The info and error output streams are each limited to 10 Kb of data per function invocation. If log messages exceed this limit, the log file will contain the first 10 Kb of data and end with the following message:
Output was too large and has been truncated.

The console object provides other methods that are supported by functions. For more information, refer to any JavaScript specification for the console object. The following additional console methods are supported:

  • assert()
  • count()
  • countReset()
  • group()
  • groupEnd()
  • time()
  • timeEnd()
  • timeLog()

Console API output

The output from most console functions is shown in the Messages section of the Logs display. For example:

Messages:

Function Start

Output from console.warn() and console.error() is shown in the Errors section, below the Messages output, as follows:

Errors:

Warning - page not found
Error - variable not defined

console.assert()

If a function is triggered on EventStream and console.assert() is called as follows:

console.assert(visitor, "visitor not defined");

The visitor object is only defined when the trigger is AudienceStream, so the assertion is false and the output is as follows:

Assertion failed: visitor not defined

console.group() and console.groupEnd()

console.group() and console.groupEnd() can be used to format related messages in the log. console.log() messages that follow console.group() are indented in the log. The indentation ends after console.groupEnd() is called.

If the function code contains the following:

console.group("Event info:");
console.log("Account: ", event.account);
console.log("Visitor ID: ", event.visitor_id);
console.groupEnd();

The output is as follows:

Event info:
  Account:  Acme Mfg
  Visitor ID:  017407a1d9e50019633c3cea732703079011607100bd6

console.time() and console.timeLog()

When a function calls console.time(), there is no output. The time that console.time() was called with that string is noted. When console.timeLog() is called with the same string, "Current Time: " in this example, the output is the specified string followed by the time elapsed since console.time() was called:

Current Time: : 1ms

After a function calls console.timeEnd(), console.timeLog() cannot be called until console.time() is called again.

console.count() and console.countReset()

Each time console.count() is called with the same string, the count is incremented. The output of console.count() is the string followed by the count. If console.count("Current count: "); is called twice, the output is as follows:

Current count: : 1
Current count: : 2

console.countReset() resets the count for the specified string to zero.

Was this page helpful?

This page was last updated: January 7, 2023