Skip to content

Commit

Permalink
Add NPS survey (#1090)
Browse files Browse the repository at this point in the history
* feat: add NPS survey

* chore: array key

* chore: use only free version install date
  • Loading branch information
Soare-Robert-Daniel authored Mar 12, 2024
1 parent 48dea95 commit 661f2a2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
75 changes: 75 additions & 0 deletions classes/Visualizer/Module/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ private function getQuery() {
*/
public function renderSupportPage() {
wp_enqueue_style( 'visualizer-upsell', VISUALIZER_ABSURL . 'css/upsell.css', array(), Visualizer_Plugin::VERSION );
$this->load_survey();
include_once VISUALIZER_ABSPATH . '/templates/support.php';
}

Expand Down Expand Up @@ -1053,6 +1054,9 @@ public function renderLibraryPage() {
'type' => 'array',
)
);

$this->load_survey();

$render->render();
}

Expand Down Expand Up @@ -1182,4 +1186,75 @@ public static function checkChartStatus( $type ) {
}
return false;
}

/**
* Get the survey metadata.
*
* @return array The survey metadata.
*/
private function get_survey_metadata() {
$install_date = get_option( 'visualizer_install', false );
$install_category = 0;

if ( false !== $install_date ) {
$days_since_install = round( ( time() - $install_date ) / DAY_IN_SECONDS );

if ( 0 === $days_since_install || 1 === $days_since_install ) {
$install_category = 0;
} elseif ( 1 < $days_since_install && 8 > $days_since_install ) {
$install_category = 7;
} elseif ( 8 <= $days_since_install && 31 > $days_since_install ) {
$install_category = 30;
} elseif ( 30 < $days_since_install && 90 > $days_since_install ) {
$install_category = 90;
} elseif ( 90 <= $days_since_install ) {
$install_category = 91;
}
}

$plugin_data = get_plugin_data( VISUALIZER_BASEFILE, false, false );
$plugin_version = '';
if ( ! empty( $plugin_data['Version'] ) ) {
$plugin_version = $plugin_data['Version'];
}

$user_id = 'visualizer_' . preg_replace( '/[^\w\d]*/', '', get_site_url() ); // Use a normalized version of the site URL as a user ID.

$license_data = get_option( 'visualizer_pro_license_data', false );
if ( false !== $license_data ) {
$user_id = 'visualizer_' . $license_data->key;
}

return array(
'userId' => $user_id,
'attributes' => array(
'days_since_install' => $install_category,
'free_version' => $plugin_version,
'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '',
'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ),
),
);
}

/**
* Load the survey.
*/
private function load_survey() {

if ( defined( 'TI_CYPRESS_TESTING' ) ) {
return;
}

$survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' );

if ( empty( $survey_handler ) ) {
return;
}

$metadata = $this->get_survey_metadata();

do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' );
wp_enqueue_script( 'visualizer_chart_survey', VISUALIZER_ABSURL . 'js/survey.js', array( $survey_handler ), $metadata['attributes']['free_version'], true );
wp_localize_script( 'visualizer_chart_survey', 'visualizerSurveyData', $metadata );
}
}
12 changes: 12 additions & 0 deletions js/survey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Initialize the formbricks survey.
*
* @see https://github.com/formbricks/setup-examples/tree/main/html
*/
window.addEventListener('themeisle:survey:loaded', function () {
window?.tsdk_formbricks?.init?.({
environmentId: "cltef8cut1s7wyyfxy3rlxzs5",
apiHost: "https://app.formbricks.com",
...(window?.visualizerSurveyData ?? {}),
});
});

0 comments on commit 661f2a2

Please sign in to comment.