track_page_visit

πŸ“… Published on

πŸ“ Last updated

Documentation

Β»

Functions

Β»

track_page_visit

Records a page visit for a contact. Groundhogg calls this automatically when a tracked contact follows a link or is identified via the tracking script. You can also call it directly to log visits from custom tracking implementations, such as a single-page app that handles its own routing.

Parameters

ParameterTypeDescription
$refstringThe URL of the page being visited.
$contactContactThe visiting contact.
$overridearrayOptional fields to override on the page visit record.

Return value

Page_Visit|false β€” The created Page_Visit object, or false on failure.

Usage

\Groundhogg\track_page_visit( get_permalink(), $contact );

Example

Here’s a pseudo code example for a hypothetical single-page app tracking integration.

add_action( 'wp_ajax_my_plugin_track_route', 'my_plugin_track_spa_route_change' );
add_action( 'wp_ajax_nopriv_my_plugin_track_route', 'my_plugin_track_spa_route_change' );

/**
 * Track a route change in a single-page app as a Groundhogg page visit.
 *
 * @return void
 */
function my_plugin_track_spa_route_change() {

    $url     = sanitize_url( $_POST['url'] ?? '' );
    $contact = \Groundhogg\get_current_contact();

    if ( ! $url || ! $contact ) {
        wp_send_json_error();
    }

    \Groundhogg\track_page_visit( $url, $contact );
    wp_send_json_success();
}

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.