Developers/Guides

Website pixel

The website pixel is a small first-party script, pb.js, that a customer's website loads. It sends page views and the events the site reports to the collector at collect.postbuddy.nl, where they become the website activity of contacts, the source of Postflows and the events journeys wait for. The pixel needs the plan features contacts and pixel; managing it needs the right pixel.manage.

The snippet

Owners, Admins and Developers copy the snippet from Developers, Website pixel (the keys and the code) or from Website, Set up (the setup for everyone else). It defines window.pb as a queue, so calls made before the script arrives are kept, and loads the script asynchronously.

HTML
<script>
  window.pb =
    window.pb ||
    function () {
      (window.pb.q = window.pb.q || []).push(arguments);
    };
  pb('init', 'pk_live_xxxxxxxxxxxxxxxx');
</script>
<script async src="https://collect.postbuddy.nl/pb.js"></script>

There is a key per mode: events sent with the test key make test cards and stay out of the live figures. Rotating a key stops sites with the old key within a minute.

The choice made in the portal travels in the snippet; the script fetches nothing at runtime.

ChoiceSnippetWhat the script does
autopb('init', key)Waits for the cookie banner: Google Consent Mode (ad_storage granted in a consent update on the dataLayer; a default, also one limited to regions, never counts) or the IAB TCF signal with purposes 1, 3 and 4. Nothing is sent or stored before that.
manualpb('init', key, { consent: 'manual' })Waits for the site's own pb('consent', true). pb('consent', false) withdraws: the queue is dropped and the cookies removed.
nonepb('init', key, { consent: 'none' })Runs without cookies. The visitor and the session live as long as the page, nothing is stored on the device, and the events of the page visit are sent at once. Identities only come from pb= and identify.

Global Privacy Control is always a no: a browser that sends it gets nothing sent, whatever the banner or the choice says.

Under auto and manual the script writes two cookies once consent is there: _pb_id (the visitor id, 13 months, never renewed silently) and _pb_sid (the session, 30 minutes rolling).

Commands

TypeScript
pb('track', 'added_to_cart', { sku: 'A1', value: 29.95 });
pb('identify', { customerId: 'crm-1042', signature: '<from your server>' });
pb('page');
pb('reset');
pb('consent', true);
  • track sends an event with up to twenty properties (strings, numbers, booleans; nested objects are dropped). Event names are lower case with underscores. The standard names are page_view, viewed_product, added_to_cart, started_checkout, placed_order, submitted_form and signed_up; own names work too.
  • identify gives the visitor the externalId of a contact, with the signature your server made for it (see Signed identity below). Every event of that visitor, earlier ones included, joins the contact.
  • page sends a page view by hand; route changes of a single-page app send one automatically.
  • reset forgets the customer id after a logout; the device stays known.

Signed identity

A customer id or an e-mail hash names a contact only with a signature made on your server: HMAC-SHA256 of the value, in hex, with the organisation's identity secret. Make the secret in the portal (Website, Set up, Recognise); it is shown once, and a new one replaces it. Never put the secret in a page: the page only passes the signature your server rendered into it.

TypeScript
// Node.js, on your server
const signature = crypto
  .createHmac('sha256', process.env.POSTBUDDY_PIXEL_SECRET)
  .update(customerId)
  .digest('hex');
PHP
// PHP, on your server
$signature = hash_hmac('sha256', $customerId, POSTBUDDY_PIXEL_SECRET);

The page then calls pb('identify', { customerId, signature }); for an e-mail, pb('identify', { emailHash, signature }) with the SHA-256 of the trimmed, lower-case address and its signature. Without a valid signature the value is kept on the events as the property unsigned_customer_id or unsigned_email_hash and never identifies. The card token from pb= identifies without a signature, as before. The collector also reads the site from the request's Origin header, not from the batch.

Rules without code

Most sites need no track calls at all. A rule turns a click, a page address or a form submit into an event, and the script fetches the enabled rules once per page from https://collect.postbuddy.nl/v1/rules/<key> (cached five minutes, so a change works within minutes without pasting the snippet again). The picker in the portal makes rules by clicking on your own site; the API makes the same rules:

HTTP
POST /v1/pixel/rules
{
  "name": "placed_order",
  "label": "Thank-you page",
  "kind": "page",
  "path": { "op": "contains", "value": "/thank-you" },
  "captures": [
    { "property": "value", "source": "amount", "value": ".order-total" },
    { "property": "order_id", "source": "query", "value": "order" },
    { "property": "currency", "source": "fixed", "value": "EUR" }
  ]
}
  • kind is click (a selector the clicked element or one of its parents matches, with text as a check on the element's words, compared without case and extra spaces), page (the path of the page matches: contains, equals or starts; the query is ignored) or form (the form matches the selector; the event fires at submit). A path on a click or form rule limits it to those pages.
  • captures fill the event's properties: fixed (the value as given), text (the text of the element the selector finds), amount (the first amount in that text, in Dutch or English notation, as a number), query (a parameter of the page's address) and field (a form field by name, form rules only). Every event carries the rule's id as rule.
  • enabled switches a rule without removing it. GET /v1/pixel/rules lists the rules with fired7d, how often each fired in the last seven days; PATCH and DELETE /v1/pixel/rules/{id} change and remove them. At most 200 rules per organisation; the names page_view and identify are the script's own.

Checking a page

POST /v1/pixel/check with { "url": "https://www.example.com/" } fetches the page (redirects followed, private addresses refused) and answers what it found (live, test, other_key, script_only or none), the consent setting the snippet carries, whether Google Tag Manager is on the page, the shop platform it recognised, the events of the last seven days and a list of issues such as snippet_missing, test_key_on_site, domain_not_allowed or no_events_manual_consent. The portal's wizard shows the same check with what to do about each issue. GET /v1/pixel carries progress: the step reached of install, check, measure and recognise.

Shop platforms

GET /v1/pixel/platform-code?mode=live returns the ready code per shop platform with the key, the collector and the consent setting filled in: shopify.theme (the snippet for theme.liquid) and shopify.customPixel (a custom pixel for Settings, Customer events, sending viewed_product, added_to_cart, started_checkout and placed_order with the order's amount and the buyer's e-mail as a SHA-256 hash, consent source platform), woocommerce.plugin (one PHP file; the portal's Install step downloads it as the zip WordPress installs) and lightspeed.head and lightspeed.order (the snippet for Custom head code and the order code for Tracking). On any page, pb('identify', { email }) hashes a buyer's e-mail in the browser and pb('identify', { emailHash, signature }) takes a hash signed on the server. The WooCommerce plugin signs the order's e-mail hash with the secret from define('POSTBUDDY_PIXEL_SECRET', '...') in wp-config.php. Shopify's custom pixel and Lightspeed's order code run in the buyer's browser, where no secret can be kept: their orders are stored and counted for the visitor (known when they arrived through a card's pb= link), but the e-mail never names a contact.

Each contact can have one recognition link on the redirect domain, https://ptqrco.de/c/<token>, for e-mail, text messages and newsletters. POST /v1/contacts/{id}/link with { "days": 30, "destination": "https://shop.example/offer" } makes it or renews it (a valid link keeps its address and gets the new term; a revoked or expired one gets a new address); GET reads it with url, active, expiresAt and clicks; POST /v1/contacts/{id}/link/revoke ends the recognition. POST /v1/contacts/links with { "contactIds": [...], "days": 90 } does up to 1,000 contacts at once and lists the ones skipped (objected or unknown). The link forwards to its own page, else linkDestination from PATCH /v1/pixel, else the first named domain; ?to=/path or a full address on the named domains opens another page. While valid it adds pb=<token>, which pb.js keeps like a card's token; the recognition carries via link. An objection revokes the link. The links need contacts.manage and the plan features contacts and pixel.

Known visitors

A visitor becomes a contact in three ways. The visitor arrives through a card: the QR code and the personal link of a card redirect with ?pb=<token> appended (the switch Add pb= to QR and link destinations in the portal), the script keeps that token on the visitor and removes it from the address, and the collector resolves it to the card and to the contact at its address. The site calls identify with the externalId it gave the contact and the signature of it. Or the site sends an e-mail hash signed on its server. A form rule with an e-mail field (emailField, or the field the script recognises by its type, autocomplete or name) hashes the e-mail in the browser, only under consent; that hash is unsigned, so it is kept with the visit and recognises no contact. Rules never read password or hidden fields, card details or fields named like them.

An unknown customer id is kept; when a contact with that externalId appears later, the job pixel.resolve joins the events.

Domains

While no domain is named, the collector accepts every host and lists the ones seen, so the member can confirm them. Once domains are named, events from other hosts are refused and counted on the host.

The collector

POST https://collect.postbuddy.nl/v1/events takes the batches the script sends as text/plain; it answers 204 (stored), 400 (not a batch), 410 (the key is gone: the script stops), 413 (too large) or 429 (more than 120 batches a minute from one address). The address is truncated before it is stored (/24 for IPv4, /48 for IPv6). Bots by user agent are dropped.

Postflows started by a website event

A Postflow always takes items through POST /v1/postflows/{id}/items and from integrations. Besides, it can have one automatic trigger: a website event with a rule (the event, up to ten conditions on its properties, a count within days, the absence of a later event, and a cooldown per contact). For a Postflow without a journey of its own, set the rule as pixelTrigger on POST /v1/postflows or PATCH /v1/postflows/{id} (null removes it); a changed rule is published as a new version of the Postflow's simple journey at once.

In a journey the rule lives in the start step: {"type": "trigger", "config": {"source": "api", "automatic": {"type": "websiteEvent", "rule": {...}}}}, or "automatic": null for none. It is part of the draft and goes live when the journey is published; PATCH with pixelTrigger then answers flow_managed. Publishing checks the rule (trigger_rule_invalid) and the pixel in the plan (feature_missing). Cards already on their way go on; a pending trigger of a rule that changed ends without a card.

Settings through the API

CallRightWhat it does
GET /v1/pixelpixel.manageThe keys, domains, hosts seen, the pb= switch, the consent choice, the snippet, counts.
PATCH /v1/pixelpixel.managedomains, appendToken and consent (auto, manual or none).
POST /v1/pixel/keys/{mode}/rotatepixel.manageA new key for the mode.
GET /v1/pixel/eventspixel.manageThe stored events, cursor paged, with filters; property values are masked without personal_data.read.
GET /v1/pixel/livepixel.manageThe last hour per minute, the share of events with a known contact and the newest fifty events.

Events are kept for the organisation's retention days (90 by default) and purged nightly.

Tag Manager

In Google Tag Manager, add a Custom HTML tag with the snippet and fire it on Initialization - All Pages. With Consent Mode on the container, keep the choice auto: the script reads the consent state from the dataLayer. With another banner, add pb('consent', true) to the tag that fires when the visitor accepts marketing cookies and choose manual. Events from the site's own code go through pb('track', ...) as above, or through a Custom HTML tag per event.