Skip to content

Commit 9df988f

Browse files
feat: compute install category as common data (#288)
1 parent 2f5ad49 commit 9df988f

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/Modules/Script_loader.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,19 @@ public function load_survey( $handler, $data = array() ) {
145145
true
146146
);
147147

148-
$data = array_replace_recursive( $this->get_survey_common_data(), $data );
148+
$data = array_replace_recursive( $this->get_survey_common_data( $data ), $data );
149149

150150
wp_localize_script( $handler, 'tsdk_survey_data', $data );
151151
}
152152

153153
/**
154154
* Get the common data in the Formbrick survey format.
155155
*
156+
* @param array $reference_data Reference data to extrapolate common properties.
157+
*
156158
* @return array
157159
*/
158-
public function get_survey_common_data() {
160+
public function get_survey_common_data( $reference_data = array() ) {
159161
$language = apply_filters( 'themeisle_sdk_current_lang', get_user_locale() );
160162
$available_languages = [
161163
'de_DE' => 'de',
@@ -168,15 +170,47 @@ public function get_survey_common_data() {
168170
if ( isset( $url_parts['path'] ) ) {
169171
$clean_url .= $url_parts['path'];
170172
}
171-
$user_id = 'u_' . hash( 'crc32b', $clean_url );
173+
$user_id = 'u_' . hash( 'crc32b', $clean_url );
172174

173-
return [
175+
$common_data = [
174176
'userId' => $user_id,
175177
'apiHost' => 'https://app.formbricks.com',
176178
'attributes' => [
177179
'language' => $lang_code,
178180
],
179181
];
182+
183+
if (
184+
isset( $reference_data['attributes'], $reference_data['attributes']['install_days_number'] )
185+
&& is_int( $reference_data['attributes']['install_days_number'] )
186+
) {
187+
$common_data['attributes']['days_since_install'] = $this->install_time_category( $reference_data['attributes']['install_days_number'] );
188+
}
189+
190+
return $common_data;
191+
}
192+
193+
/**
194+
* Compute the install time category.
195+
*
196+
* @param int $install_days_number The number of days passed since installation.
197+
*
198+
* @return int The category.
199+
*/
200+
private function install_time_category( $install_days_number ) {
201+
$install_category = 0;
202+
203+
if ( 1 < $install_days_number && 8 > $install_days_number ) {
204+
$install_category = 7;
205+
} elseif ( 8 <= $install_days_number && 31 > $install_days_number ) {
206+
$install_days_number = 30;
207+
} elseif ( 30 < $install_days_number && 90 > $install_days_number ) {
208+
$install_category = 90;
209+
} elseif ( 90 <= $install_days_number ) {
210+
$install_category = 91;
211+
}
212+
213+
return $install_category;
180214
}
181215

182216
/**

tests/script-loader-test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,17 @@ function() {
189189
}
190190
);
191191

192-
$data = $script_loader->get_survey_common_data();
192+
$data = $script_loader->get_survey_common_data( [ 'attributes' => [ 'install_days_number' => 3 ] ] );
193193

194194
// Assert the structure and content of returned data
195195
$this->assertIsArray( $data );
196196
$this->assertArrayHasKey( 'userId', $data );
197197
$this->assertArrayHasKey( 'attributes', $data );
198198
$this->assertArrayHasKey( 'language', $data['attributes'] );
199+
$this->assertArrayHasKey( 'days_since_install', $data['attributes'] );
200+
201+
// Test install category.
202+
$this->assertEquals( 7, $data['attributes']['days_since_install'] );
199203

200204
// Test German language mapping
201205
$this->assertEquals( 'de', $data['attributes']['language'] );

0 commit comments

Comments
 (0)