Skip to content

Commit

Permalink
Move buildButtonClick to plugin
Browse files Browse the repository at this point in the history
- export `removeEmptyProperties`
  • Loading branch information
greg-el committed Nov 22, 2023
1 parent 1d72a8a commit 0a927b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
38 changes: 1 addition & 37 deletions libraries/tracker-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,42 +851,6 @@ export function buildLinkClick(event: LinkClickEvent): PayloadBuilder {
return buildSelfDescribingEvent({ event: eventJson });
}

/**
* A Button Click event
*
* Used when a user clicks on a <button> tag on a webpage
* <input type="submit"> tags are intentionally not tracked by this event,
* as this functionality is provided by the Form Tracking plugin
* see: https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/javascript-trackers/web-tracker/plugins/form-tracking/
*/
export interface ButtonClickEvent {
/** The text on the button clicked, or a user-provided override */
label: string;
/** The ID of the button clicked, if present */
id?: string;
/** An array of class names from the button clicked */
classes?: Array<string>;
/** The name of the button, if present */
name?: string;
}

/**
* Build a Button Click Event
* Used when a user clicks on a <button> tag on a webpage
*
* @param event - Contains the properties for the Button Click event
* @returns PayloadBuilder to be sent to {@link @snowplow/tracker-core#TrackerCore.track}
*/
export function buildButtonClick(event: ButtonClickEvent): PayloadBuilder {
const { label, id, classes, name } = event;
const eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/button_click/jsonschema/1-0-0',
data: removeEmptyProperties({ label, id, classes, name }),
};

return buildSelfDescribingEvent({ event: eventJson });
}

/**
* An Ad Impression Event
* Used to track an advertisement impression
Expand Down Expand Up @@ -1402,7 +1366,7 @@ export function buildConsentGranted(event: ConsentGrantedEvent) {
* @param exemptFields - Set of fields which should not be removed even if empty
* @returns A cleaned copy of eventJson
*/
function removeEmptyProperties(
export function removeEmptyProperties(
event: Record<string, unknown>,
exemptFields: Record<string, boolean> = {}
): Record<string, unknown> {
Expand Down
37 changes: 37 additions & 0 deletions plugins/browser-plugin-button-click-tracking/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PayloadBuilder, buildSelfDescribingEvent, removeEmptyProperties } from '@snowplow/tracker-core';

/**
* A Button Click event
*
* Used when a user clicks on a <button> tag on a webpage
* <input type="submit"> tags are intentionally not tracked by this event,
* as this functionality is provided by the Form Tracking plugin
* see: https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/javascript-trackers/web-tracker/plugins/form-tracking/
*/
export interface ButtonClickEvent {
/** The text on the button clicked, or a user-provided override */
label: string;
/** The ID of the button clicked, if present */
id?: string;
/** An array of class names from the button clicked */
classes?: Array<string>;
/** The name of the button, if present */
name?: string;
}

/**
* Build a Button Click Event
* Used when a user clicks on a <button> tag on a webpage
*
* @param event - Contains the properties for the Button Click event
* @returns PayloadBuilder to be sent to {@link @snowplow/tracker-core#TrackerCore.track}
*/
export function buildButtonClick(event: ButtonClickEvent): PayloadBuilder {
const { label, id, classes, name } = event;
const eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/button_click/jsonschema/1-0-0',
data: removeEmptyProperties({ label, id, classes, name }),
};

return buildSelfDescribingEvent({ event: eventJson });
}

0 comments on commit 0a927b3

Please sign in to comment.