import { BrowserPlugin } from "@snowplow/browser-tracker-core"; import { SelfDescribingJson } from "@snowplow/tracker-core"; /** * Like `Required`, but any keys in `E` are optional instead of required. */ type RequiredExcept = { [P in Exclude]-?: Exclude; } & { [P in E]?: T[P]; }; /** * A thing, or an array of thing. Just a neater interface for the user writing configuration. */ type OneOrMany = T | T[]; /** * An attribute list details the results of running DataSelectors on an element. * * It's a list of key-value pairs along with a label for the source DataSelector to remove ambiguity. * The list of objects is chosen for BigQuery compat, as it doesn't really support arbitrary "map" types. */ type AttributeList = { source: string; attribute: string; value: string; }[]; /** * A DataSelector defines information to extract from an element. * * It can be a custom function returning arbitrary key/values as an object. Non-string values will be cast to string or cast to JSON strings. If exceptions are thrown, the error information is captured. * Alternatively there are several declarative options that can be specified as object properties. * * The properties allowed are: * - attributes: Return the values of a list of attributes of the element * - properties: Return the values of a list of property names of the element's DOM node; this is similar to attributes in some cases but different in others. E.g. `class` as an attribute needs to be `className` as a property; some attributes will reflect their initial value rather than what has been updated via JavaScript * - dataset: Return the values of a list of data-* attributes; uses the camelCase name rather than the kebab-case name of the attribute * - selector: Specify `true` to attach the CSS selector used to match the element; can be used to differentiate elements with the same `name` * - content: Provide an object mapping names to RegExp patterns to run on the element's text content. If it matches it is included. The first group in the pattern will be prioritized if specified. * - child_text: Provide an object mapping names to CSS selectors; the selectors are evaluated against the element and the first matching element's text content is the value. * - match: Logical operator for use when used as a condition; always evaluated last. Look at existing matches so far, comparing each key/value to the provided object. Alternatively supply a predicate function that determines if this matches or not. If there are no matches, discard any matches found to this point. */ type DataSelector = ((element: Element) => Record) | { attributes: string[]; } | { properties: string[]; } | { dataset: string[]; } | { selector: boolean; } | { content: Record; } | { child_text: Record; } | { match: Record boolean)>; }; /** * When this type of event should actually be tracked after it has been detected. */ declare enum Frequency { /** * Track this event every time it occurs; e.g. EXPOSE every time it scrolls into/out of view. */ ALWAYS = "always", /** * Track this event at most once per element that matches the selector for the lifetime of the plugin. */ ELEMENT = "element", /** * Only track the event the first time it occurs per configuration. Even if other elements would trigger the event, ignore them after the first time it occurs. */ ONCE = "once", /** * Never track this event, effectively disabling a configuration. */ NEVER = "never", /** * Track each event only once per element until the next pageview is seen, allowing it to be tracked again. Mostly useful for Single Page Applications. */ PAGEVIEW = "pageview" } declare enum ConfigurationState { INITIAL = 0, CONFIGURED = 1 } /** * A dynamic context provider for events generated from the plugin. * * Can be a static list of Self Describing JSON entities, or a function that returns the same. * The function will receive the matching element, and matching element configuration as parameters. */ type ContextProvider = SelfDescribingJson[] | ((element: Element | HTMLElement | undefined, match: Configuration) => SelfDescribingJson[]); /** * Options controlling when this type of event should occur. */ type BaseOptions = { /** * Frequency cap options for how often this should be tracked over the lifetime of the plugin. */ when: `${Frequency}`; /** * A custom DataSelector defining if an element should trigger the event or not. If the DataSelector returns no result triplets, the event does not trigger. The `match` operation can be used here to do some logic against the other types of operators. */ condition?: DataSelector; }; /** * Additional options for controlling when an EXPOSE event should occur. */ type ExposeOptions = BaseOptions & { /** * For larger elements, only trigger if at least this proportion of the element is visible on screen; expects: 0.0 - 1.0; default: 0 */ minPercentage?: number; /** * Only trigger once the element has been in view for at least this many milliseconds. The time is measured cumulatively. After the threshold is met it will re-fire immediately. */ minTimeMillis?: number; /** * Don't count this element as visible unless its area (height * width) is at least this many pixels. Useful to prohibit empty container elements being tracked as visible. */ minSize?: number; /** * Add these dimensions (in pixels) to the element size when calculating minPercentage. Used to increase/decrease the size of the actual element before considering it visible. */ boundaryPixels?: [ number, number, number, number ] | [ number, number ] | number; }; /** * Input configuration format for describing a set of elements to be tracked with this plugin. */ type ElementConfiguration = { /** * Logical name for elements matched by this configuration. This name will be used to describe any matching elements in event payloads and entities, and to associate the data between them. * If not provided, the `selector` is used as `name`. */ name?: string; /** * Required. CSS selector to determine the set of elements that match this configuration. */ selector: string; /** * If `selector` is intended for matching elements within custom elements or shadow DOM hosts, specify a selector for the shadow hosts here; this will be used to identify shadow-elements that match `selector` that would otherwise not be visible. */ shadowSelector?: string; /** * If using `shadowSelector` to indicate `selector` matches elements in shadow hosts; use this to specify that only elements within shadow hosts matching `shadowSelector` should match; if `false` (default), elements outside shadow hosts (that are not necessarily children of `shadowSelector` hosts) will match the configuration also. */ shadowOnly?: boolean; /** * Configure when, if ever, element create events should be triggered when detected for elements matching this configuration. * Defaults to `false`, which is shorthand for `{ when: 'never' }`. */ create?: boolean | BaseOptions; /** * Configure when, if ever, element destroy events should be triggered when detected for elements matching this configuration. * Defaults to `false`, which is shorthand for `{ when: 'never' }`. */ destroy?: boolean | BaseOptions; /** * Configure when, if ever, element expose events should be triggered when detected for elements matching this configuration. * Also specify additional criteria on relevant for expose events like minimum size or visibility time. * Defaults to `true`, which is shorthand for `{ when: 'always' }` and `0` for all other options. */ expose?: boolean | ExposeOptions; /** * Configure when, if ever, element obscure events should be triggered when detected for elements matching this configuration. * Defaults to `false`, which is shorthand for `{ when: 'never' }`. */ obscure?: boolean | BaseOptions; /** * Indicate that elements matching this configuration are "components"; their ancestry of other elements will be identified in the component_parents entity if this is set (using this configuration's `name`). */ component?: boolean; /** * When events occur to elements matching this configuration, extract data from one or more DataSelectors and include them in `attributes` in the `element` entity that describes that element. */ details?: OneOrMany; /** * When events occur to elements matching this configuration, evaluate one or more nested configurations using the matching element as a root; the `name`, `selector`, `details`, and `contents` will be processed, with other options ignored. The resulting elements (and optionally their `details` will be included as entities on events for this element.) */ contents?: OneOrMany; /** * Types of events that statistics for the element should be included on, if any. Should be the `event_name` value for events that should have the entity attached. E.g. `page_view`, `page_ping`, `expose_element`, `my_custom_event`. */ includeStats?: OneOrMany; /** * Provide custom context entities for events generated from this configuration. */ context?: ContextProvider; /** * An optional ID for this configuration. * Calls to track configurations with a specific ID will override previous configurations with the same ID. * No impact on actual tracking or payloads. */ id?: string; }; /** * Parsed valid version of `ElementConfiguration`. * Removes some ambiguities allowed in that type that are there for a more pleasant configuration API. */ type Configuration = Omit, "create" | "destroy" | "expose" | "obscure" | "details" | "includeStats" | "contents"> & { trackers?: string[]; create: BaseOptions; destroy: BaseOptions; expose: RequiredExcept; obscure: BaseOptions; state: ConfigurationState; details: DataSelector[]; includeStats: string[]; contents: Configuration[]; context: Extract; }; declare enum Events { ELEMENT_CREATE = "iglu:com.snowplowanalytics.snowplow/create_element/jsonschema/1-0-0", ELEMENT_DESTROY = "iglu:com.snowplowanalytics.snowplow/destroy_element/jsonschema/1-0-0", ELEMENT_EXPOSE = "iglu:com.snowplowanalytics.snowplow/expose_element/jsonschema/1-0-0", ELEMENT_OBSCURE = "iglu:com.snowplowanalytics.snowplow/obscure_element/jsonschema/1-0-0" } declare enum Entities { ELEMENT_DETAILS = "iglu:com.snowplowanalytics.snowplow/element/jsonschema/1-0-0", ELEMENT_CONTENT = "iglu:com.snowplowanalytics.snowplow/element_content/jsonschema/1-0-0", ELEMENT_STATISTICS = "iglu:com.snowplowanalytics.snowplow/element_statistics/jsonschema/1-0-0", COMPONENT_PARENTS = "iglu:com.snowplowanalytics.snowplow/component_parents/jsonschema/1-0-0" } type SDJ> = SelfDescribingJson & { schema: S; }; type ElementDetailsEntity = SDJ; type ComponentsEntity = SDJ; /** * Parameters for startElementTracking. */ type ElementTrackingConfiguration = { /** * Optional context generator or static contexts to apply/add to any events generated by this batch of element configurations. */ context?: ContextProvider; /** * Single or array of element configurations to start tracking events for. */ elements: OneOrMany; }; /** * Parameters for endElementTracking. */ type ElementTrackingDisable = { /** * A list of configuration names to stop tracking. Configurations can share names so this may match multiple cases. * Configurations with no explicit name use their selector as their name. */ elements: OneOrMany; } | { /** * A list of configuration IDs to stop tracking. Only a single Configuration is allowed to exist per ID so this can be used to target specific instances. */ elementIds: OneOrMany>; } | { /** * Custom predicate to return if each Configuration should be removed (`true`) or kept (`false`). * @param configuration The filter function to decide if the Configuration should be removed or not. * @returns */ filter: (configuration: Readonly) => boolean; }; /** * Plugin for tracking the addition and removal of elements to a page and the visibility of those elements. * @param param0 Plugin configuration. * @param param0.ignoreNextPageView Only required when use per-pageview frequency configurations and the ordering vs the pageview event matters. Defaults to `true`, which means the next pageview event will be ignored and not count as resetting the per-pageview state; this is correct if you're calling startElementTracking before calling trackPageView. * @returns */ declare function SnowplowElementTrackingPlugin({ ignoreNextPageView }?: { ignoreNextPageView?: boolean | undefined; }): BrowserPlugin; /** * Start Element tracking for elements that match the given configuration(s). * * Invalid configurations will be ignored, but valid configurations in the same batch will still apply. * * You can call this multiple times with different batches of configurations. E.g. section-specific configs, different custom context, different tracker instance destinations. * Configurations supplied in multiple calls will not be deduped unless an `id` is provided and it collides with previous `id` values. * * @param param0 Element Tracking configuration options containing a batch of element configurations and optionally, custom context. * @param trackers A list of tracker instance names that should receive events generated by this batch of element configurations. If not provided, events go to all trackers the plugin has activated for. * @returns */ declare function startElementTracking({ elements, context }: ElementTrackingConfiguration, trackers?: Array): void; /** * Stop tracking events for the configurations with the given names or IDs, or satisfying a custom predicate. * If no parameters provided, removes all previous configurations. * All element configurations have names, if not provided the `name` is the `selector` which is required. * * No considerations are made for tracker instance destinations. Use IDs or filters if you need specific routing options. * @param remove Filter information for which element configurations should be removed. Omit to remove all configurations. */ declare function endElementTracking(remove?: ElementTrackingDisable): void; declare const componentGenerator: (...args: any[]) => ComponentsEntity | null; declare const detailedComponentGenerator: (...args: any[]) => [ ComponentsEntity, ...ElementDetailsEntity[] ] | null; /** * Obtain access to functions that can determine details about any configured `component`s and return `component_parents` context for given elements. * * It returns two generator functions: * - one returns a single `component_parents` entity, which lists the names of any defined components that are ancestors of the provided element * - the second returns multiple entities, including `component_parents` plus any element_details information about each matching component * * When called, the generators will examine all parameters in order looking for: * - an element to look for owning components of * - a string to use as the element_name in the component_parents entity * * The functions are suitable for use in Dynamic Context Generator functions used in other plugins such as Link and Form tracking. * * If a `cb` function is provided, it is called with the above generators as parameters that it can use in asynchronous situations (such as the JavaScript tracker). * * @param cb Callback function to receive the generator callbacks described above asynchronously. * @returns Array of callbacks described above. */ declare function getComponentListGenerator(cb?: (basic: typeof componentGenerator, detailed: typeof detailedComponentGenerator) => void): [ typeof componentGenerator, typeof detailedComponentGenerator ]; export { ElementTrackingConfiguration, ElementTrackingDisable, SnowplowElementTrackingPlugin, startElementTracking, endElementTracking, getComponentListGenerator };