Skip to content

Commit

Permalink
Merge pull request #961 from Codeinwp/feat/formbricks
Browse files Browse the repository at this point in the history
feat: add formbricks
  • Loading branch information
vytisbulkevicius authored May 22, 2024
2 parents d8bcce1 + 6c72e8c commit 4edf3d0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"plugins": [ "." ],
"env": {
"tests": {
"config": {
"TI_TESTING": true
}
}
}
}
65 changes: 65 additions & 0 deletions includes/admin/class-rop-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ public function enqueue_scripts() {
wp_deregister_script( 'vue-libs' );
}

$this->register_survey();
}

/**
Expand Down Expand Up @@ -1784,4 +1785,68 @@ public static function rop_check_reached_sharing_limit( $sharing_type = 'tw' ) {
}
return false;
}

/**
* Get the data used for the survey.
*
* @return array The survey metadata.
*/
public function get_survey_metadata() {
$license_data = get_option( 'tweet_old_post_pro_license_data', array() );
$attributes = array();
$user_id = 'rop_' . ( ! empty( $license_data->key ) ? $license_data->key : preg_replace( '/[^\w\d]*/', '', get_site_url() ) ); // Use a normalized version of the site URL as a user ID for free users.

$days_since_install = round( ( time() - get_option( 'rop_first_install_date', 0 ) ) / DAY_IN_SECONDS );
$install_category = 0; // Normalized value.
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;
}

$attributes['days_since_install'] = strval( $install_category );
$attributes['license_status'] = ! empty( $license_data->license ) ? $license_data->license : 'invalid';
$attributes['free_version'] = $this->version;

if ( ! empty( $license_data->plan ) ) {
$attributes['plan'] = strval( $license_data->plan );
}

if ( defined( 'ROP_PRO_VERSION' ) ) {
$attributes['pro_version'] = ROP_PRO_VERSION;
}

return array(
'userId' => $user_id,
'attributes' => $attributes,
);
}

/**
* Register the survey script.
*
* It does not register if we are in a testing environment.
*
* @return void
*/
public function register_survey() {

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

$survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' );
if ( empty( $survey_handler ) ) {
return;
}

do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' );
wp_localize_script( $survey_handler, 'ropSurveyData', $this->get_survey_metadata() );
}
}
13 changes: 13 additions & 0 deletions vue/src/rop_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ window.addEventListener( 'load', function () {
},
} );
} );

/**
* 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: "clwgcs7ia03df11mgz7gh15od",
apiHost: "https://app.formbricks.com",
...(window?.ropSurveyData ?? {}),
});
});

0 comments on commit 4edf3d0

Please sign in to comment.