Client Data

Client Data

HTML
<script>
  SP.init("WEBSITE_ID", function () {
    var client = SP.getClient();
    SP.addTag('vip');
    if (SP.hasSegment('returning_customers')) {
      // ...
    }
  });
</script>

Trustisto keeps the client in the browser. You can read it with the methods below, e.g. to personalize your page, and change the client’s tags and scoring. To save client details (e-mail, phone, consents), use Save Client.

Client

  • SP.getClient() - the saved client: the fields from Save Client, plus uid (the visitor ID), tags, segments and scoring. Returns {} when nothing is saved.

Tags, segments and scoring

Every client has tags, segments and a scoring. Trustisto sets them on its own based on events (as configured in the panel), and you can also read and change them from JavaScript.

When the client is known (identified by e-mail or phone), the changes are synced with Trustisto. Otherwise they are kept in the browser and synced once the client is identified.

Tags

  • SP.addTag(tag) - add one tag
  • SP.addTags([tag1, tag2]) - add a few tags
  • SP.removeTag(tag) - remove one tag
  • SP.removeTags([tag1, tag2]) - remove a few tags
  • SP.getTags() - all tags of the client, as an array
  • SP.hasTag(tag) - true if the client has the tag
  • SP.hasTags([tag1, tag2]) - true if the client has all the tags
  • SP.hasAnyTags([tag1, tag2]) - true if the client has at least one of the tags

Tags are normalized: characters other than letters, digits, _ and spaces are removed, and spaces are replaced with _. So "New customer!" becomes New_customer.

Segments

Segments are assigned by Trustisto, based on the segment rules defined in the panel. From JavaScript you can only read them:

  • SP.getSegments() - all segments of the client, as an array
  • SP.hasSegment(segment) - true if the client is in the segment
  • SP.hasSegments([segment1, segment2]) - true if the client is in all the segments
  • SP.hasAnySegments([segment1, segment2]) - true if the client is in at least one of the segments

Scoring

  • SP.getScoring() - the client’s current scoring
  • SP.changeScoring(points) - add points to the scoring; pass a negative number to subtract

Watch form fields

HTML
<script>
  SP.go('watchClient', '#checkout input[name="email"]', 'email');
  SP.go('watchClient', '#checkout input[name="firstname"]', 'firstname');
</script>

watchClient saves a client field whenever the value of a form field changes, so you don’t need to call Save Client yourself. It is handy on checkout and registration forms: the client is identified as soon as they type their e-mail, even if they never finish the order. It also works for the coupon code field.

JavaScript
SP.watchClient(selector, field, readOnStart, getValue)
  • selector - CSS selector of the input(s) to watch. Inputs that are not on the page yet are looked for every 2.5 seconds, until they appear.
  • field - the client field to save, e.g. email, phone, firstname, marketing.mail, couponCode
  • readOnStart - true saves the current value as soon as the input is found (default: false)
  • getValue - function that returns the value from the input (default: checked for checkboxes, value for other inputs)

Empty values are skipped. Each input is watched only once, even if you call watchClient for it again.

Stop watching

HTML
<script>
  SP.go('clearWatchClient');
</script>

clearWatchClient removes all watchers. It is also called automatically by recheckUrlTriggers.