Skip to content
Merged
3 changes: 3 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
--health-retries 10

steps:
- name: Install SVN
run: sudo apt-get install -y subversion

- name: Checkout code
uses: actions/checkout@v4

Expand Down
41 changes: 41 additions & 0 deletions assets/src/js/integrations/form-submit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Plausible Analytics
*
* Track Form Submissions JS
*/
document.addEventListener('DOMContentLoaded', () => {
let plausible_track_form_submit = {
forms: document.querySelectorAll('form'),

/**
* Initialization.
*/
init: function () {
this.bindEvents();
},

/**
* Bind Events.
*/
bindEvents: function () {
let self = this;

this.forms.forEach((form) => {
form.addEventListener('submit', (e) => {
if (e.target.checkValidity()) {
self.trackSubmission();
}
})
})
},

/**
* Send a custom event to Plausible.
*/
trackSubmission: function () {
plausible(plausible_analytics_i18n.form_completions, {'props': {'form': document.location.pathname}});
}
};

plausible_track_form_submit.init();
});
29 changes: 17 additions & 12 deletions src/Actions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
/**
* Plausible Analytics | Actions.
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
Expand All @@ -12,7 +11,6 @@
class Actions {
/**
* Constructor.
*
* @since 1.0.0
* @access public
* @return void
Expand All @@ -24,9 +22,8 @@ public function __construct() {
}

/**
* This <meta> tag "tells" the Plausible API which version of the plugin is used, to allow tailored error messages, specific to the plugin
* version.
*
* This <meta> tag "tells" the Plausible API which version of the plugin is used, to allow tailored error messages,
* specific to the plugin version.
* @return void
*/
public function insert_version_meta_tag() {
Expand All @@ -37,7 +34,6 @@ public function insert_version_meta_tag() {

/**
* Register Assets.
*
* @since 1.0.0
* @access public
* @return void
Expand All @@ -56,9 +52,16 @@ public function maybe_register_assets() {
}

$version =
Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) : PLAUSIBLE_ANALYTICS_VERSION;
Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) :
PLAUSIBLE_ANALYTICS_VERSION;

wp_enqueue_script( 'plausible-analytics', Helpers::get_js_url( true ), '', $version, apply_filters( 'plausible_load_js_in_footer', false ) );
wp_enqueue_script(
'plausible-analytics',
Helpers::get_js_url( true ),
'',
$version,
apply_filters( 'plausible_load_js_in_footer', false )
);

// Goal tracking inline script (Don't disable this as it is required by 404).
wp_add_inline_script(
Expand All @@ -77,7 +80,7 @@ public function maybe_register_assets() {
);

/**
* Documentation.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
* document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
*/
$data = str_replace( '"document.location.pathname"', 'document.location.pathname', $data );

Expand All @@ -95,14 +98,17 @@ public function maybe_register_assets() {
[
'props' => [
// convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together
'search_query' => strtolower(trim(get_search_query())),
'search_query' => strtolower( trim( get_search_query() ) ),
'result_count' => $wp_query->found_posts,
],
]
);
$script = "plausible('WP Search Queries', $data );";

wp_add_inline_script( 'plausible-analytics', "document.addEventListener('DOMContentLoaded', function() {\n$script\n});" );
wp_add_inline_script(
'plausible-analytics',
"document.addEventListener('DOMContentLoaded', function() {\n$script\n});"
);
}

// This action allows you to add your own custom scripts!
Expand All @@ -111,7 +117,6 @@ public function maybe_register_assets() {

/**
* Create admin bar nodes.
*
* @since 1.3.0
* @access public
*
Expand Down
38 changes: 19 additions & 19 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
/**
* Plausible Analytics | Provisioning.
*
* @since 2.0.0
* @package WordPress
* @subpackage Plausible Analytics
Expand Down Expand Up @@ -68,21 +67,20 @@ public function __construct( $client = null ) {
}

$this->custom_event_goals = [
'404' => __( '404', 'plausible-analytics' ),
'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ),
'file-downloads' => __( 'File Download', 'plausible-analytics' ),
'search' => __( 'WP Search Queries', 'plausible-analytics' ),
'404' => __( '404', 'plausible-analytics' ),
'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ),
'file-downloads' => __( 'File Download', 'plausible-analytics' ),
'search' => __( 'WP Search Queries', 'plausible-analytics' ),
'form-completions' => __( 'Form Completions', 'plausible-analytics' ),
];

$this->init();
}

/**
* Action & filter hooks.
*
* @return void
* @throws ApiException
*
* @codeCoverageIgnore
*/
private function init() {
Expand Down Expand Up @@ -210,11 +208,11 @@ private function create_goals( $goals ) {
* @param $settings
*
* @return void
*
* @codeCoverageIgnore Because we don't want to test the API.
*/
public function maybe_create_woocommerce_funnel( $old_settings, $settings ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) {
if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) ||
! Integrations::is_wc_active() ) {
return; // @codeCoverageIgnore
}

Expand Down Expand Up @@ -254,7 +252,6 @@ public function maybe_create_woocommerce_funnel( $old_settings, $settings ) {
* @param $steps
*
* @return void
*
* @codeCoverageIgnore Because this method should be mocked in tests if needed.
*/
private function create_funnel( $name, $steps ) {
Expand Down Expand Up @@ -325,14 +322,13 @@ public function maybe_delete_goals( $old_settings, $settings ) {
}

/**
* Delete all custom WooCommerce event goals if Revenue setting is disabled. The funnel is deleted when the minimum required no. of goals is no
* longer met.
* Delete all custom WooCommerce event goals if Revenue setting is disabled. The funnel is deleted when the minimum
* required no. of goals is no longer met.
*
* @param $old_settings
* @param $settings
*
* @return void
*
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_delete_woocommerce_goals( $old_settings, $settings ) {
Expand Down Expand Up @@ -361,14 +357,13 @@ public function maybe_delete_woocommerce_goals( $old_settings, $settings ) {
}

/**
* Searches an array for the presence of $string within each element's value. Strips currencies using a regex, e.g. (USD), because these are
* added to revenue goals by Plausible.
* Searches an array for the presence of $string within each element's value. Strips currencies using a regex, e.g.
* (USD), because these are added to revenue goals by Plausible.
*
* @param string $string
* @param array $haystack
*
* @return false|mixed
*
* @codeCoverageIgnore Because it can't be unit tested.
*/
private function array_search_contains( $string, $haystack ) {
Expand All @@ -390,15 +385,15 @@ private function array_search_contains( $string, $haystack ) {
* @param array $settings
*
* @return void
*
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_create_custom_properties( $old_settings, $settings ) {
$enhanced_measurements = $settings[ 'enhanced_measurements' ];

if ( ! Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) &&
! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) &&
! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) {
! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) &&
! Helpers::is_enhanced_measurement_enabled( 'form-completions', $enhanced_measurements ) ) {
return; // @codeCoverageIgnore
}

Expand All @@ -417,7 +412,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
/**
* Create Custom Properties for WooCommerce integration.
*/
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) && Integrations::is_wc_active() ) {
if ( Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) &&
Integrations::is_wc_active() ) {
foreach ( WooCommerce::CUSTOM_PROPERTIES as $property ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
}
Expand All @@ -432,6 +428,10 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
}
}

if ( Helpers::is_enhanced_measurement_enabled( 'form-completions', $enhanced_measurements ) ) {
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => 'form' ] ] );
}

if ( empty( $properties ) ) {
return; // @codeCoverageIgnore
}
Expand Down
Loading
Loading