Skip to content

Commit 661f2a2

Browse files
Add NPS survey (#1090)
* feat: add NPS survey * chore: array key * chore: use only free version install date
1 parent 48dea95 commit 661f2a2

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ private function getQuery() {
900900
*/
901901
public function renderSupportPage() {
902902
wp_enqueue_style( 'visualizer-upsell', VISUALIZER_ABSURL . 'css/upsell.css', array(), Visualizer_Plugin::VERSION );
903+
$this->load_survey();
903904
include_once VISUALIZER_ABSPATH . '/templates/support.php';
904905
}
905906

@@ -1053,6 +1054,9 @@ public function renderLibraryPage() {
10531054
'type' => 'array',
10541055
)
10551056
);
1057+
1058+
$this->load_survey();
1059+
10561060
$render->render();
10571061
}
10581062

@@ -1182,4 +1186,75 @@ public static function checkChartStatus( $type ) {
11821186
}
11831187
return false;
11841188
}
1189+
1190+
/**
1191+
* Get the survey metadata.
1192+
*
1193+
* @return array The survey metadata.
1194+
*/
1195+
private function get_survey_metadata() {
1196+
$install_date = get_option( 'visualizer_install', false );
1197+
$install_category = 0;
1198+
1199+
if ( false !== $install_date ) {
1200+
$days_since_install = round( ( time() - $install_date ) / DAY_IN_SECONDS );
1201+
1202+
if ( 0 === $days_since_install || 1 === $days_since_install ) {
1203+
$install_category = 0;
1204+
} elseif ( 1 < $days_since_install && 8 > $days_since_install ) {
1205+
$install_category = 7;
1206+
} elseif ( 8 <= $days_since_install && 31 > $days_since_install ) {
1207+
$install_category = 30;
1208+
} elseif ( 30 < $days_since_install && 90 > $days_since_install ) {
1209+
$install_category = 90;
1210+
} elseif ( 90 <= $days_since_install ) {
1211+
$install_category = 91;
1212+
}
1213+
}
1214+
1215+
$plugin_data = get_plugin_data( VISUALIZER_BASEFILE, false, false );
1216+
$plugin_version = '';
1217+
if ( ! empty( $plugin_data['Version'] ) ) {
1218+
$plugin_version = $plugin_data['Version'];
1219+
}
1220+
1221+
$user_id = 'visualizer_' . preg_replace( '/[^\w\d]*/', '', get_site_url() ); // Use a normalized version of the site URL as a user ID.
1222+
1223+
$license_data = get_option( 'visualizer_pro_license_data', false );
1224+
if ( false !== $license_data ) {
1225+
$user_id = 'visualizer_' . $license_data->key;
1226+
}
1227+
1228+
return array(
1229+
'userId' => $user_id,
1230+
'attributes' => array(
1231+
'days_since_install' => $install_category,
1232+
'free_version' => $plugin_version,
1233+
'pro_version' => defined( 'VISUALIZER_PRO_VERSION' ) ? VISUALIZER_PRO_VERSION : '',
1234+
'license_status' => apply_filters( 'product_visualizer_license_status', 'invalid' ),
1235+
),
1236+
);
1237+
}
1238+
1239+
/**
1240+
* Load the survey.
1241+
*/
1242+
private function load_survey() {
1243+
1244+
if ( defined( 'TI_CYPRESS_TESTING' ) ) {
1245+
return;
1246+
}
1247+
1248+
$survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' );
1249+
1250+
if ( empty( $survey_handler ) ) {
1251+
return;
1252+
}
1253+
1254+
$metadata = $this->get_survey_metadata();
1255+
1256+
do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' );
1257+
wp_enqueue_script( 'visualizer_chart_survey', VISUALIZER_ABSURL . 'js/survey.js', array( $survey_handler ), $metadata['attributes']['free_version'], true );
1258+
wp_localize_script( 'visualizer_chart_survey', 'visualizerSurveyData', $metadata );
1259+
}
11851260
}

js/survey.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Initialize the formbricks survey.
3+
*
4+
* @see https://github.com/formbricks/setup-examples/tree/main/html
5+
*/
6+
window.addEventListener('themeisle:survey:loaded', function () {
7+
window?.tsdk_formbricks?.init?.({
8+
environmentId: "cltef8cut1s7wyyfxy3rlxzs5",
9+
apiHost: "https://app.formbricks.com",
10+
...(window?.visualizerSurveyData ?? {}),
11+
});
12+
});

0 commit comments

Comments
 (0)