// Code by MonsterInsights start.
function monsterinsights_custom_redirect_pageview() {
if ( function_exists( 'monsterinsights_get_uuid' ) && is_singular() ) {
if ( ! monsterinsights_track_user() ) {
return false;
}
$params = array(
'page_location' => get_permalink( get_the_ID() ),
'page_title' => get_the_title( get_the_ID() ),
);
$post_author = get_post_field( 'post_author', get_the_ID() );
$dimensions = monsterinsights_get_option( 'custom_dimensions', array() );
if ( ! empty( $dimensions ) && is_array( $dimensions ) ) {
foreach ( $dimensions as $dimension ) {
$value = '';
if ( empty( $dimension['type'] ) || empty( $dimension['id'] ) ) {
continue;
}
$type = $dimension['type'];
if ( 'author' === $type ) {
$firstname = get_the_author_meta( 'user_firstname', $post_author );
$lastname = get_the_author_meta( 'user_lastname', $post_author );
if ( ! empty( $firstname ) || ! empty( $lastname ) ) {
$value = trim( $firstname . ' ' . $lastname );
} else {
$value = 'user-' . get_the_author_meta( 'ID', $post_author );
}
}
if ( 'category' === $type ) {
if ( monsterinsights_is_wp_seo_active() ) {
$main_category = get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true );
if ( ! empty( $main_category ) ) {
$main_category = get_category( $main_category );
if ( ! empty( $main_category->name ) ) {
$value = $main_category->name;
}
}
}
if ( empty( $value ) ) {
$categories = get_the_category( get_the_ID() );
if ( $categories ) {
foreach ( $categories as $category ) {
$category_names[] = $category->slug;
}
$value = implode( ',', $category_names );
}
}
}
if ( 'tags' === $type ) {
if ( is_single() ) {
$value = 'untagged';
$tags = get_the_tags( get_the_ID() );
if ( $tags ) {
$value = implode( ',', wp_list_pluck( $tags, 'name' ) );
}
}
}
if ( 'published_at' === $type ) {
$value = get_the_date( 'c', get_the_ID() );
}
if ( ! empty( $value ) ) {
$params[ $type ] = $value;
}
}
}
monsterinsights_mp_collect_v4( array(
'events' => array(
array(
'name' => 'page_view',
'params' => $params,
)
)
) );
}}
// Code by MonsterInsights end.
/* Redirects post to redirected url */
add_action(‘template_redirect’, ‘redirect_post_link’);
function redirect_post_link() {
if (is_singular(‘post’)):
global $post;
$link = get_field(‘original_url’, $post->ID);
//echo $post->ID;
if (isset($link) && $link != ”) {
//echo $link;
monsterinsights_custom_redirect_pageview(); // Here we added the GA4 tracking function.
wp_redirect(esc_url($link . ‘?utm_source=elinfonet’), 301);
exit;
}
endif;
}