Datafly.js SDKConfiguration

Configuration

This page documents all configuration options for Datafly.js, including init parameters, per-source settings, vendor configuration, and the context API.

init() options

When using the npm package, pass a configuration object to init():

import datafly from '@datafly/collector';
 
datafly.init({
  pipelineKey: 'dk_live_abc123',
  endpoint: 'https://data.example.com',
  batchSize: 10,
  flushInterval: 1000,
  crossDomain: {
    hubUrl: 'https://id.company.com/resolve',
    linkedDomains: ['brand-a.com', 'brand-b.com'],
  },
});

Options reference

OptionTypeDefaultDescription
pipelineKeystring(required)The pipeline key for this source, issued from the management UI. Format: dk_live_... or dk_test_...
endpointstring(required)The Ingestion Gateway URL (your first-party subdomain, e.g., https://data.example.com)
batchSizenumber10Maximum number of events per batch request
flushIntervalnumber1000Auto-flush interval in milliseconds
crossDomainCrossDomainConfigundefinedCross-domain identity configuration (see below)

CrossDomainConfig

OptionTypeDefaultDescription
hubUrlstring(required)The Identity Hub resolve endpoint URL
linkedDomainsstring[][]List of domains to link (sent to hub for informational purposes)

When using the script tag method, the pipelineKey and endpoint are automatically resolved from the script tag’s data-pipeline-key attribute and src origin. Additional configuration (batch size, cross-domain, etc.) is loaded from the per-source config stored on the server.

Script tag attributes

When loading Datafly.js via script tag, the following attributes are supported:

<script
  src="https://data.example.com/d.js"
  data-pipeline-key="dk_live_abc123"
  data-global="_df"
></script>
AttributeDefaultDescription
src(required)URL to the Datafly.js script on your subdomain
data-pipeline-key(required)The pipeline key for this source
data-global_dfThe name of the global variable exposed on window

Custom global variable

If _df conflicts with another library on your page, change the global variable name:

<script src="https://data.example.com/d.js" data-pipeline-key="dk_live_abc123" data-global="_myAnalytics"></script>
<script>
  _myAnalytics.page();
  _myAnalytics.track('Button Clicked');
</script>

Custom script filename

The default script filename is d.js. This is configured per source in the management UI under Sources > Build Settings > Script Filename. The filename is intentionally short and generic to minimise the chance of being blocked by ad blockers.

You can change it to any filename:

FilenameURL
d.js (default)https://data.example.com/d.js
collector.jshttps://data.example.com/collector.js
t.jshttps://data.example.com/t.js

Per-source configuration

Each source has a configuration stored in PostgreSQL that controls its behaviour. This is managed through the management UI or the Management API.

Source config structure

{
  "source_id": "src_abc123",
  "name": "Production Website",
  "pipeline_key": "dk_live_abc123",
  "endpoint": "https://data.example.com",
  "global_variable": "_df",
  "script_filename": "d.js",
  "vendors": {
    "ga4": {
      "enabled": true,
      "measurement_id": "G-XXXXXXXXXX"
    },
    "meta": {
      "enabled": true,
      "pixel_id": "1234567890"
    },
    "tiktok": {
      "enabled": false,
      "pixel_id": null
    },
    "pinterest": {
      "enabled": true,
      "tag_id": "1234567890"
    },
    "snapchat": {
      "enabled": false,
      "pixel_id": null
    },
    "linkedin": {
      "enabled": false,
      "partner_id": null
    },
    "google_ads": {
      "enabled": true,
      "conversion_id": "AW-1234567890"
    }
  },
  "consent": {
    "mode": "auto",
    "default_state": {
      "analytics": false,
      "marketing": false,
      "functional": false
    }
  },
  "cross_domain": {
    "enabled": false,
    "hub_url": null,
    "linked_domains": []
  },
  "client_tags": {},
  "debug": false
}

Vendor configuration

Each vendor can be enabled or disabled per source, with vendor-specific metadata:

Google Analytics 4

{
  "ga4": {
    "enabled": true,
    "measurement_id": "G-XXXXXXXXXX"
  }
}
FieldDescription
measurement_idYour GA4 Measurement ID (found in GA4 > Admin > Data Streams)

Meta (Facebook)

{
  "meta": {
    "enabled": true,
    "pixel_id": "1234567890"
  }
}
FieldDescription
pixel_idYour Meta Pixel ID (found in Events Manager)

TikTok

{
  "tiktok": {
    "enabled": true,
    "pixel_id": "CXXXXXXXXXXXXXXXXX"
  }
}
FieldDescription
pixel_idYour TikTok Pixel ID (found in TikTok Events Manager)

Pinterest

{
  "pinterest": {
    "enabled": true,
    "tag_id": "1234567890"
  }
}
FieldDescription
tag_idYour Pinterest Tag ID (found in Pinterest Ads Manager)

Snapchat

{
  "snapchat": {
    "enabled": true,
    "pixel_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  }
}
FieldDescription
pixel_idYour Snapchat Pixel ID (found in Snap Ads Manager)

LinkedIn

{
  "linkedin": {
    "enabled": true,
    "partner_id": "1234567"
  }
}
FieldDescription
partner_idYour LinkedIn Partner ID (found in LinkedIn Campaign Manager)
{
  "google_ads": {
    "enabled": true,
    "conversion_id": "AW-1234567890"
  }
}
FieldDescription
conversion_idYour Google Ads Conversion ID (found in Google Ads > Tools > Conversions)

Client tags

Client tags are custom key-value pairs that are added to every event sent from this source. Use them to tag events with metadata such as environment, version, or business unit:

{
  "client_tags": {
    "environment": "production",
    "app_version": "2.4.1",
    "business_unit": "ecommerce",
    "region": "eu-west"
  }
}

Client tags appear in the event payload under context.client_tags:

{
  "type": "track",
  "event": "Order Completed",
  "context": {
    "client_tags": {
      "environment": "production",
      "app_version": "2.4.1",
      "business_unit": "ecommerce",
      "region": "eu-west"
    }
  }
}

Context API

The context API allows you to set custom key-value pairs that are merged into every event’s context object. Unlike client tags (which are set at the source level), context values are set at runtime from your JavaScript code.

context.set()

_df.context.set(key, value)
ParameterTypeDescription
keystringThe context key
valueunknownThe context value (any serialisable type)
_df.context.set('experiment_id', 'exp-42');
_df.context.set('page_category', 'product_listing');
_df.context.set('ab_variant', 'control');

context.unset()

Remove a previously set context key:

_df.context.unset('experiment_id');

context.clear()

Remove all custom context values:

_df.context.clear();
⚠️

The library key in the context is reserved and cannot be overridden via context.set(). It always contains { name: "@datafly/collector", version: "0.1.0" }.

Debug mode

Enable debug mode to log all events to the browser console:

{
  "debug": true
}

Or when using the npm package:

datafly.init({
  pipelineKey: 'dk_live_abc123',
  endpoint: 'https://data.example.com',
  debug: true,
});

In debug mode, Datafly.js logs:

  • Every event as it is enqueued
  • Every batch as it is sent
  • Consent state changes
  • Vendor ID collection results
  • Cross-domain resolution steps
  • Transport errors (normally silent)
⚠️

Do not enable debug mode in production. It writes verbose output to the browser console and may expose pipeline keys and event data to anyone with developer tools open.

Pipeline key formats

Pipeline keys follow a consistent format that indicates the environment:

PrefixEnvironmentDescription
dk_live_ProductionEvents are processed and delivered to vendors
dk_test_TestingEvents are processed but not delivered to vendors

Test pipeline keys are useful for staging environments where you want to verify event collection without sending data to live vendor accounts.

Transport details

SettingValueDescription
Batch size10 events maxEvents are sent in batches of up to 10
Flush interval1,000 msQueue is flushed every second
Primary transportnavigator.sendBeaconFire-and-forget, survives page unload
Fallback transportXMLHttpRequestUsed when sendBeacon is unavailable
Endpoint path/v1/batchBatch endpoint on the Ingestion Gateway
Auth headerBasic {base64(pipelineKey:)}Pipeline key sent via Authorization header (XHR) or query param (sendBeacon)
Unload flushvisibilitychange + pagehideEvents are flushed when the page is hidden or unloaded

Helper methods

flush()

Force an immediate flush of all queued events:

_df.flush();

getAnonymousId()

Get the current anonymous ID:

const anonId = _df.getAnonymousId();
// "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"

getUserId()

Get the current user ID (set via identify()):

const userId = _df.getUserId();
// "user-123" or undefined if not yet identified

getConsent()

Read the current consent state:

const consent = _df.getConsent();
// { analytics: true, marketing: false, functional: true }