Ga naar inhoud

Measurement hooks

Deze inhoud is nog niet vertaald.

The Measurement add-on sends events, such as page loads, submissions and payments, to analytics providers. A provider runs either in the browser (client) or on the server (server), and Google Tag Manager is built in. With these hooks you can add providers, settings fields for them, and moments.

Filter · includes/gravityforms/addons/class-measurementaddon.php

Collects the available measurement providers. Without a provider the Measurement add-on is not available. The result is cached per form for the request. The per-form filter cs_forms_measurement_feed_providers_{form_id} runs first.

Parameter Type Description
$providers array<string, array> Providers keyed by ID. Each provider has id, name and supported_types (client and/or server). Optional keys: suggested_keys (an array of choices, or a callable returning them), extra_feed_settings (settings fields, or a callable returning them), feed_extra_options (callable ($feed, $form, $addon) returning extra options sent with the events) and handle_server_feed (callable ($feed, $entry, $form, $addon) returning the result or a WP_Error).
$form array|null The form object, or null outside a form context.

Returns: array<string, array> — the providers.

add_filter('cs_forms_measurement_feed_providers', function ($providers, $form) {
$providers['my_analytics'] = [
'id' => 'my_analytics',
'name' => __('My Analytics', 'my-plugin'),
'supported_types' => ['server'],
'handle_server_feed' => function ($feed, $entry, $form, $addon) {
$response = wp_remote_post('https://analytics.example.com/collect', [
'body' => ['event' => 'form_submit', 'form_id' => rgar($form, 'id')],
]);
return is_wp_error($response) ? $response : 'sent';
},
];
return $providers;
}, 10, 2);

cs_forms_measurement_feed_providers_{form_id}

Section titled “cs_forms_measurement_feed_providers_{form_id}”

Filter · includes/gravityforms/addons/class-measurementaddon.php

The same filter for a single form, for example cs_forms_measurement_feed_providers_12. It runs before cs_forms_measurement_feed_providers and only when a form is known.

Parameter Type Description
$providers array<string, array> The providers so far. Default [].
$form array The form object.

Returns: array<string, array> — the providers.

add_filter('cs_forms_measurement_feed_providers_12', function ($providers, $form) {
unset($providers['my_analytics']);
return $providers;
}, 10, 2);

Filter · includes/gravityforms/addons/class-measurementaddon.php

Collects the fields of the Measurement settings page. Providers use it to add their own settings, such as an ID or endpoint. It starts as an empty array, so a callback can add fields to it or return its own list.

Parameter Type Description
$settings array<string, array> Setting definitions keyed by setting name. Default []. Each has type (such as text, header or separator), and usually title and description.
$addon \CampaignSuite\Forms\Includes\GravityForms\Addons\MeasurementAddon The Measurement add-on.

Returns: array<string, array> — the setting definitions.

add_filter('cs_forms_measurement_settings', function ($settings, $addon) {
$settings['MY_ANALYTICS_KEY'] = [
'title' => __('My Analytics key', 'my-plugin'),
'type' => 'text',
'description' => __('Used for server side events.', 'my-plugin'),
];
return $settings;
}, 10, 2);

Filter · includes/gravityforms/addons/class-measurementaddon.php

Filters the moments at which a measurement feed can run. The built-in moments are on_load_all_pages, on_submit_all_pages, on_load_page_{N}, on_submit_page_{N} (for multi-page forms) and on_confirmation_load. The FinDock integration adds the findock_* moments through this filter. The per-form filter cs_forms_measurement_feed_moments_{form_id} runs first. The result is cached per form for the request.

Parameter Type Description
$moments array<string, array> Moments keyed by ID. Each has label, description, types (client and/or server) and entry_required (whether an entry exists at that moment).
$form array The form object.

Returns: array<string, array> — the moments.

add_filter('cs_forms_measurement_feed_moments', function ($moments, $form) {
$moments['my_review_done'] = [
'label' => __('Review completed', 'my-plugin'),
'description' => __('Runs when your own code triggers this moment.', 'my-plugin'),
'types' => ['server'],
'entry_required' => true,
];
return $moments;
}, 10, 2);

cs_forms_measurement_feed_moments_{form_id}

Section titled “cs_forms_measurement_feed_moments_{form_id}”

Filter · includes/gravityforms/addons/class-measurementaddon.php

The same filter for a single form, for example cs_forms_measurement_feed_moments_12. It runs before cs_forms_measurement_feed_moments.

Parameter Type Description
$moments array<string, array> The moments so far, in the shape described above.
$form array The form object.

Returns: array<string, array> — the moments.

add_filter('cs_forms_measurement_feed_moments_12', function ($moments, $form) {
unset($moments['on_load_all_pages']);
return $moments;
}, 10, 2);

cs_forms_measurement_can_reexecute_feed_on_success

Section titled “cs_forms_measurement_can_reexecute_feed_on_success”

Filter · includes/gravityforms/addons/class-measurementaddon.php

Decides whether a measurement feed that already succeeded for an entry may run again. By default it may not. Feeds on the repeatable moments on_load_all_pages and on_submit_all_pages can always run again, and the filter isn’t called for them or for forced runs.

Parameter Type Description
$can_reexecute bool Default false.
$feed array The measurement feed.
$entry array The entry.
$form array The form object.
$previous_result mixed The stored result of the earlier run.

Returns: bool — true to run the feed again.

add_filter('cs_forms_measurement_can_reexecute_feed_on_success', function ($can_reexecute, $feed, $entry, $form, $previous_result) {
return rgars($feed, 'meta/feed_moment') === 'on_confirmation_load' ? true : $can_reexecute;
}, 10, 5);