Quick Start on WordPress
Here’s a concise guide for your WordPress telemetry script:
Code
<?php
if (!defined('ABSPATH')) {
exit;
}
function inject_telemetry_script() {
// Get the settings
$options = get_option('yofi_telemetry_settings');
$public_token = $options['public_token'] ?? '';
$journey_id_salt = $options['journey_id_salt'] ?? '';
// Only proceed if we have the required settings
if (empty($public_token) || empty($journey_id_salt)) {
return;
}
$user_id = ""; // Optional
$seller_id = ""; // Optional
?>
<script>
window.__yofi_telemetry_data = {
public_token: <?php echo json_encode($public_token); ?>,
journey_id_salt: <?php echo json_encode($journey_id_salt); ?>,
user_id: <?php echo json_encode($user_id); ?>,
seller_id: <?php echo json_encode($seller_id); ?>
// Set `access_token` for using journey ID and client ID across domains if need.
};
window.YofiSessionInstance = {
addLabels: (labels) => {
window.__yofi_telemetry_data.labels = {
...labels,
};
},
};
</script>
<script defer src="<?php echo esc_url('https://static-resources.yofi.ai/sdk/yofi-telemetry.js'); ?>"></script>
<?php
}
add_action('wp_head', 'inject_telemetry_script', 5);
function interact_with_telemetry_from_wp_sample() {
?>
<script>
// YofiSessionInstance
YofiSessionInstance.addLabels({
userId: 'your_label_value',
});
// Example for retrieving an access token
async function getAccessToken() {
const accessToken = await YofiTelemetry.getAppAccessToken();
return accessToken;
}
</script>
<?php
}
add_action('wp_head', 'interact_with_telemetry_from_wp_sample', 6);1. Add inject_telemetry_script (Required)
Why: This function is mandatory to set up the Yofi telemetry system. It:
Loads the telemetry settings (
public_token,journey_id_salt).Creates
YofiSessionInstancefor adding labels.Includes the external
yofi-telemetry.jsscript.
$options = get_option('yofi_telemetry_settings');
$public_token = $options['public_token'] ?? '';
$journey_id_salt = $options['journey_id_salt'] ?? '';This part also can be replaced with inline hardcode on PHP for PoC but we suggest reading from settings for security purposes.
What Happens Without It: No telemetry—no data, no tracking, nothing works.
How: Hooked to wp_head with priority 5 to run early.
2. Use interact_with_telemetry_from_wp_sample (Example)
What It Does: This is a sample showing how to add custom labels (e.g.,
userId: 'your_label_value') to track actions.Condition for Success: It must run after
inject_telemetry_script:inject_telemetry_script(priority5) definesYofiSessionInstance.interact_with_telemetry_from_wp_sample(priority6) uses it.Priority Rule: Use a priority higher than
5(e.g.,6,10) inadd_action('wp_head', ...)to ensureYofiSessionInstanceis ready. If it’s5or lower, it’ll fail with an error like “YofiSessionInstance is not defined.”
Summary
Must-Do: Add
inject_telemetry_scriptto initialize telemetry (priority5).Optional: Use
YofiSessionInstance.addLabels()later (priority >5) to track stuff, as shown in the sample.
Last updated
Was this helpful?