Skip to Content
AI Powered Docs! 🤖 These docs are translated with AI, so keep an eye out for minor quirks. We're always improving!
TechnicalWordpressTracking Events

Sending user data and events through WordPress

The Metrix plugin enables you to send user information and desired events directly to Metrix through WordPress. You can do this using PHP or JavaScript.

Sending data using PHP

Sending Data and Events to Metrix via WordPress Server and Theme

If you want to send data from the server side, simply call the required Metrix SDK methods in the appropriate location within your theme or plugin.

In the following example, using the wp_footer hook, the current user is retrieved and their information is sent to Metrix:

// after website being loaded call send_user function add_action( 'wp_footer', 'send_user' ); function send_user() { $current_user = wp_get_current_user(); if ( $current_user->user_email ) { Metrix_SDK::authorize_user( $current_user->user_email ); Metrix_SDK::set_email( $current_user->user_email ); } }

Another way to call the SDK methods is to invoke them from WordPress templates. To do this, simply call the method in the template file. For example:

<?php Metrix_SDK::new_event("EVENT_SLUG", array( 'name' => "PAGE_NAME" )) ?>

In the above example, an event is first created in the dashboard, and an attribute named name is added to it. Its slug is placed in the new_event method, and you should set PAGE_NAME as the name of the page you want to send (this value can be dynamic so that a specific name can be set for each page).

<?php $attributes = array( 'page_name' => 'HomePage', 'user_type' => 'Guest', ); Metrix_SDK::new_event_by_name('PAGE_VISIT', $attributes); ?>

In the example above, an event was first created in the Metrix dashboard, and an attribute named name was defined for it. Then, using the new_event_by_name method, this event is triggered in the WordPress template. In this method, the value PAGE_NAME is sent as the name of the page you wish to record. This value can be dynamic so that different values (such as page title, category, or content type) can be sent for each page.

Sending Data After Form Submission

For example, you want to send the data submitted through a form to Metrix as an event. To do this, you need to define a form. Place a PHP code above the form so that if the form is submitted, the value in the form is sent to us via the SDK.

<?php if ( isset( $_POST['search'] ) ) { if ( isset( $_POST['search_value'] ) ) { // ... Metrix_SDK::new_event("YOUR_EVENT_SLUG", array( 'search' => $_POST['search_value'] ) ); } } ?\> <form method="post"> <input name="search_value"> <input type="submit" name="search" value="Search"> </form>

Sending data using Javascript

If you need to send events from the client side (front-end) based on user interactions (such as clicking a button), you can use the Metrix web SDK methods. There is no need to install the SDK separately; simply call the newEvent method using JavaScript code. For more details, refer to the web SDK documentation.

Types of Methods for Sending User Data

The following is a list of methods related to sending user information:

Metrix_SDK::authorize_user( $user_id ); Metrix_SDK::set_first_name( $first_name ); Metrix_SDK::set_last_name( $last_name ); Metrix_SDK::set_email( $email ); Metrix_SDK::set_hashed_email( $hashed_email ); Metrix_SDK::set_phone_number( $phone_number ); Metrix_SDK::set_hashed_phone_number( $hashd_phone_number ); Metrix_SDK::set_country( $country ); Metrix_SDK::set_city( $city ); Metrix_SDK::set_region( $region ); Metrix_SDK::set_locality( $locality ); Metrix_SDK::set_gender( $birthday ); Metrix_SDK::set_custom_attribute( $key, $value ); Metrix_SDK::set_custom_user_id( $id );

Sending Events

Sending an event using a slug
To send an event, simply call the new_event method along with the event slug and any desired attributes.

$custom_attributes = array( 'manufacturer' => 'Nike', 'product_name' => 'Shirt', ); // attributes are optional Metrix_SDK::new_event( $slug, $custom_attributes ); // if your event doesn't have any attribute only send slug like this Metrix_SDK::new_event( $slug );

Sending Events by Name
To send an event, simply call the new_event_by_name method along with the event name and any desired attributes.

<?php $custom_attributes = array( 'manufacturer' => 'Nike', 'product_name' => 'Shirt', ); // attributes are optional Metrix_SDK::new_event_by_name('PURCHASE_COMPLETED', $custom_attributes); // if your event doesn't have any attribute only send name like this Metrix_SDK::new_event_by_name('PURCHASE_COMPLETED', array()); ?>

Metrix stores and manages your user data. If desired, you can use the Metrix dashboard to run SMS campaigns or push notifications for your users.

Last updated on