__( 'Where your contacts come from, what they read before they buy, and what they are looking at. Click anything to see the people behind it.', 'nodebook' ) ) ); echo '

' . esc_html( /* translators: %s: time ago */ sprintf( __( 'Updated %s ago', 'nodebook' ), human_time_diff( $data['built'], time() ) ) ) . ' · ' . esc_html__( 'Refresh', 'nodebook' ) . ''; if ( ! empty( $data['capped'] ) ) { echo ' · ' . esc_html__( 'Pages and interests cover the last 12 months.', 'nodebook' ); } if ( ! empty( $pages['sampled'] ) ) { echo ' · ' . esc_html__( 'Busy site: based on your most recent 30,000 page views.', 'nodebook' ); } echo '

'; self::insight( $src, $pages['rows'], (array) $data['demand'] ); // Headline tiles: the first four open the contacts behind them. $link = static function ( $kind ) use ( $days ) { return Nodebook_Admin::url( 'nodebook', array( 'from' => $kind . ':' . $days ) ); }; $kpis = array( array( number_format_i18n( $t['leads'] ), __( 'New contacts', 'nodebook' ), $link( 'new' ), 'user-plus' ), array( number_format_i18n( $t['customers'] ), __( 'Became customers', 'nodebook' ), $link( 'buyers' ), 'check' ), array( number_format_i18n( $t['returning'] ), __( 'Came back to your site', 'nodebook' ), $link( 'back' ), 'eye' ), ); if ( $woo ) { $kpis[] = array( Nodebook_Report::money( $t['revenue'] ), __( 'Revenue from new contacts', 'nodebook' ), add_query_arg( array( 'orderby' => 'total_spent', 'order' => 'desc' ), $link( 'buyers' ) ), 'activity' ); } $measured = (int) ( $ttc['measured'] ?? 0 ); $kpis[] = array( null === $ttc['days'] ? '—' : self::days( (float) $ttc['days'] ), __( 'From first visit to converting', 'nodebook' ), '', 'clock', $measured /* translators: %s: number of contacts */ ? sprintf( _n( 'Median of %s contact', 'Median of %s contacts', $measured, 'nodebook' ), number_format_i18n( $measured ) ) : __( 'Appears once tracked visitors convert', 'nodebook' ), ); echo '
'; foreach ( $kpis as $k ) { $inner = '' . esc_html( $k[0] ) . '' . esc_html( $k[1] ) . '' . ( ! empty( $k[4] ) ? '' . esc_html( $k[4] ) . '' : '' ); if ( $k[2] ) { echo '' . $inner . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts above. } else { echo '
' . $inner . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts above. } } echo '
'; self::chart( $days ); echo '
'; // Sources: each row opens those contacts. $cols = $woo ? array( __( 'Contacts', 'nodebook' ), __( 'Bought', 'nodebook' ), __( 'Revenue', 'nodebook' ) ) : array( __( 'Contacts', 'nodebook' ), __( 'Bought', 'nodebook' ) ); echo '

' . esc_html__( 'Where contacts came from', 'nodebook' ) . '

' . esc_html__( 'First touch', 'nodebook' ) . '
'; if ( ! $src ) { echo '

' . esc_html__( 'No contacts in this period yet.', 'nodebook' ) . '

'; } else { $max = max( 1, max( wp_list_pluck( $src, 'leads' ) ) ); self::head( $cols ); echo ''; } echo '
'; // Pages read before converting: unfold to see who. echo '

' . esc_html__( 'Read before they converted', 'nodebook' ) . '

' . esc_html( /* translators: %s: number of contacts */ sprintf( _n( '%s conversion', '%s conversions', $pages['conversions'], 'nodebook' ), number_format_i18n( $pages['conversions'] ) ) ) . '
'; if ( ! $pages['rows'] ) { echo '

' . esc_html__( 'Nothing yet. This fills up when known contacts read pages on your site and then send a form, order or book. Cart, checkout and thank-you pages are left out.', 'nodebook' ) . '

'; } else { $max = max( 1, max( wp_list_pluck( $pages['rows'], 'converted' ) ) ); self::head( array( __( 'Converted', 'nodebook' ), __( 'Of readers', 'nodebook' ) ) ); echo ''; } echo '
'; // What known contacts are looking at. $demand = (array) $data['demand']; if ( $demand ) { echo '

' . esc_html( Nodebook_Niche::get( 'demand' ) ) . '

' . esc_html__( 'Known contacts', 'nodebook' ) . '
'; self::head( array( __( 'People', 'nodebook' ), array( __( 'Hot', 'nodebook' ), __( 'Viewed a high-intent page in the last 7 days', 'nodebook' ) ), __( 'Converted', 'nodebook' ) ) ); echo '
'; } // B2B: accounts, not just people. $accounts = $data['accounts']; if ( $accounts && ( 'b2b' === Nodebook_Niche::current() || count( $accounts ) >= 3 ) ) { echo '

' . esc_html__( 'Companies on your site', 'nodebook' ) . '

' . esc_html__( 'Three people from one company is a buying committee', 'nodebook' ) . '
'; self::head( array( __( 'People', 'nodebook' ), __( 'Views', 'nodebook' ), __( 'Hot', 'nodebook' ), __( 'Last seen', 'nodebook' ) ) ); echo '
'; } // Campaigns. if ( $camp ) { echo '

' . esc_html__( 'Campaigns', 'nodebook' ) . '

UTM
'; self::head( $woo ? array( __( 'Contacts', 'nodebook' ), __( 'Bought', 'nodebook' ), __( 'Revenue', 'nodebook' ) ) : array( __( 'Contacts', 'nodebook' ), __( 'Bought', 'nodebook' ) ) ); echo '
'; } echo '
'; /** * Extra report sections at the bottom of Journeys. * * @param int $days Selected period in days (0 = all time). */ do_action( 'nodebook_journeys_sections', $days ); echo '

' . esc_html__( 'Tip: add UTM tags to your ads and newsletter links (utm_source, utm_medium, utm_campaign). Google, Microsoft, Meta, TikTok and LinkedIn ad clicks are recognised automatically.', 'nodebook' ) . '

'; Nodebook_Admin::footer(); } /** Node's take on the period, in one or two lines. */ private static function insight( array $src, array $pages, array $demand ) { if ( ! Nodebook_Node::on() ) { return; } $calm = 'calm' === Nodebook_Node::personality(); $lines = array(); $best = null; foreach ( $src as $r ) { if ( $r['customers'] && '' !== (string) $r['source'] && ( ! $best || $r['customers'] > $best['customers'] ) ) { $best = $r; } } if ( $best ) { $lines[] = sprintf( /* translators: 1: source, 2: number of customers */ $calm ? __( 'Most customers came from %1$s (%2$s).', 'nodebook' ) : __( 'Your best source: %1$s, good for %2$s customers.', 'nodebook' ), Nodebook_Attribution::label( $best['source'], $best['medium'] ), number_format_i18n( $best['customers'] ) ); } if ( $pages ) { $lines[] = sprintf( /* translators: %s: page */ $calm ? __( 'Read most before converting: %s.', 'nodebook' ) : __( 'People who buy often read %s first. Make it shine ✨', 'nodebook' ), self::page_name( $pages[0]['path'] ) ); } foreach ( $demand as $r ) { if ( $r['hot'] ) { $lines[] = sprintf( $calm /* translators: 1: product or item, 2: number of people */ ? _n( '%1$s has %2$s hot visitor right now.', '%1$s has %2$s hot visitors right now.', $r['hot'], 'nodebook' ) /* translators: 1: product or item, 2: number of people */ : _n( '%1$s is hot right now: %2$s person looking 🔥', '%1$s is hot right now: %2$s people looking 🔥', $r['hot'], 'nodebook' ), $r['title'], number_format_i18n( $r['hot'] ) ); break; } } if ( ! $lines ) { $lines[] = $calm ? __( 'Not enough data in this period yet. Try a longer period.', 'nodebook' ) : __( 'Not much to go on in this period yet. Try a longer one 👀', 'nodebook' ); } echo '
'; Nodebook_Admin_Node::says( implode( ' ', $lines ), $best ? 'happy' : 'calm' ); echo '
'; } /** * New contacts over the period as a bar chart: days for 7 and 30 days, weeks for 90 days, months beyond. * * @param int $days Period in days (0 = all time). */ private static function chart( $days ) { global $wpdb; $step = 7 === $days || 30 === $days ? 'day' : ( 90 === $days ? 'week' : 'month' ); $count = 'day' === $step ? $days : ( 'week' === $step ? 13 : ( 365 === $days ? 12 : 24 ) ); $now = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- local calendar buckets. $keys = array(); for ( $i = $count - 1; $i >= 0; $i-- ) { if ( 'day' === $step ) { $ts = strtotime( "-{$i} days", $now ); $k = gmdate( 'Y-m-d', $ts ); $l = wp_date( 'j M', $ts, new DateTimeZone( 'UTC' ) ); } elseif ( 'week' === $step ) { $ts = strtotime( 'monday this week', $now ) - $i * WEEK_IN_SECONDS; $k = gmdate( 'o-W', $ts ); $l = wp_date( 'j M', $ts, new DateTimeZone( 'UTC' ) ); } else { $ts = strtotime( gmdate( 'Y-m-01', $now ) . " -{$i} months" ); $k = gmdate( 'Y-m', $ts ); $l = wp_date( 'M', $ts, new DateTimeZone( 'UTC' ) ); } $keys[ $k ] = array( $l, 0 ); } $first = 'day' === $step ? gmdate( 'Y-m-d', strtotime( '-' . ( $count - 1 ) . ' days', $now ) ) : ( 'week' === $step ? gmdate( 'Y-m-d', strtotime( 'monday this week', $now ) - ( $count - 1 ) * WEEK_IN_SECONDS ) : gmdate( 'Y-m-01', strtotime( gmdate( 'Y-m-01', $now ) . ' -' . ( $count - 1 ) . ' months' ) ) ); $off = (int) round( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table; a small grouped read. $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT DATE(created_at) AS d, COUNT(*) AS n FROM %i WHERE created_at >= %s GROUP BY DATE(created_at)', Nodebook_Contacts::table(), gmdate( 'Y-m-d H:i:s', strtotime( $first . ' 00:00:00' ) - $off ) ), ARRAY_A ); $total = 0; foreach ( $rows as $r ) { $ts = strtotime( $r['d'] . ' 12:00:00' ); $k = 'day' === $step ? gmdate( 'Y-m-d', $ts ) : ( 'week' === $step ? gmdate( 'o-W', $ts ) : gmdate( 'Y-m', $ts ) ); if ( isset( $keys[ $k ] ) ) { $keys[ $k ][1] += (int) $r['n']; $total += (int) $r['n']; } } $max = max( 1, max( wp_list_pluck( $keys, 1 ) ) ); $last = count( $keys ) - 1; $by = array( 'day' => __( 'per day', 'nodebook' ), 'week' => __( 'per week', 'nodebook' ), 'month' => __( 'per month', 'nodebook' ), ); echo '

' . esc_html__( 'New contacts', 'nodebook' ) . ' ' . esc_html( $by[ $step ] ) . '

' . esc_html( number_format_i18n( $total ) ) . '
'; echo '
'; $labels = array_values( $keys ); foreach ( array( 0, (int) floor( $last / 2 ), $last ) as $n ) { echo '' . esc_html( $labels[ $n ][0] ) . ''; } echo '
'; } /** "/producto/caminador-de-batman/" → "producto › caminador de batman" ("/" → "Home page"). */ private static function page_name( $path ) { $path = trim( (string) $path, '/' ); if ( '' === $path ) { return __( 'Home page', 'nodebook' ); } $parts = explode( '/', $path ); $last = str_replace( array( '-', '_' ), ' ', urldecode( (string) array_pop( $parts ) ) ); return ( $parts ? implode( ' › ', array_map( 'urldecode', $parts ) ) . ' › ' : '' ) . $last; } private static function days( $d ) { if ( $d < 1 ) { return __( 'Same day', 'nodebook' ); } $n = (int) round( $d ); /* translators: %s: number of days */ return sprintf( _n( '%s day', '%s days', $n, 'nodebook' ), number_format_i18n( $n ) ); } /** * Column labels above a list, aligned with the numbers in each row. * * @param array $cols Labels; a label can be [ label, explanation ]. */ private static function head( array $cols ) { echo ''; } private static function bar( $value, $max ) { $w = max( 3, (int) round( (float) $value / max( 1, (float) $max ) * 100 ) ); return ''; } /** Names of the people listed under pages and products (one query; only contacts this user may see). */ private static function names( array $pages, array $demand ) { $ids = array(); foreach ( array_merge( $pages, $demand ) as $r ) { foreach ( (array) ( $r['who'] ?? array() ) as $id ) { $ids[ (int) $id ] = true; } } if ( ! $ids ) { return array(); } $out = array(); $res = Nodebook_Contacts::query( array( 'ids' => array_keys( $ids ), 'per_page' => count( $ids ), ) ); foreach ( $res['rows'] as $c ) { $out[ (int) $c['id'] ] = Nodebook_Contacts::display_name( $c ); } return $out; } /** The unfolded part of a row: who, as links to their profiles, plus a link to the page itself. */ private static function people( array $ids, array $names, $url, $open_label ) { echo '
'; $shown = 0; foreach ( $ids as $id ) { if ( ! isset( $names[ (int) $id ] ) ) { continue; } $name = $names[ (int) $id ]; echo '' . esc_html( $name ) . ''; ++$shown; } if ( ! $shown ) { echo '' . esc_html__( 'Nobody you can see.', 'nodebook' ) . ''; } if ( $url ) { echo '' . esc_html( $open_label ) . ' ↗'; } echo '
'; } }