Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpaternina9/b4e1944d6a1940d984fd432d5faf1e05 to your computer and use it in GitHub Desktop.
Save dpaternina9/b4e1944d6a1940d984fd432d5faf1e05 to your computer and use it in GitHub Desktop.
<?php
function monsterinsights_custom_redirect_pageview()
{
if (function_exists('monsterinsights_get_uuid') && is_singular()) {
if (!monsterinsights_track_user()) {
return false;
}
$event = [
'name' => 'page_view',
'params' => [
'page_location' => get_permalink(get_the_ID()),
'page_title' => get_the_title(get_the_ID())
]
];
$additional_params = [];
$post_author = get_post_field('post_author', get_the_ID());
$dimensions = monsterinsights_get_option('custom_dimensions', array());
if (!empty($dimensions) && is_array($dimensions)) {
// Sort by array key `id` value.
$id = array();
foreach ($dimensions as $key => $row) {
if (empty($row['type']) || empty($row['id'])) {
unset($dimensions[$key]);
continue;
}
$id[$key] = $row['id'];
}
array_multisort($id, SORT_ASC, $dimensions);
foreach ($dimensions as $dimension) {
if (empty($dimension['type']) || empty($dimension['id'])) {
continue;
}
$type = $dimension['type'];
$id = $dimension['id'];
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) {
$value = '';
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 = $date = get_the_date('c', get_the_ID());
}
if (!empty($value)) {
$additional_params[$type] = $value;
}
}
}
$event['params'] = array_merge(
$event['params'],
$additional_params
);
$atts = array(
'events' => [
$event
]
);
monsterinsights_mp_collect_v4($atts);
}
}
/* Redirects post to redirected url */
add_action('template_redirect', 'redirect_post_link');
if ( !function_exists('redirect_post_link') ) {
function redirect_post_link()
{
if (is_singular('post')):
global $post;
$link = get_field('redirect_url', $post->ID);
if (isset($link) && $link != '') {
monsterinsights_custom_redirect_pageview();
wp_redirect(esc_url($link . '?utm_source=elinfonet'), 301);
exit;
}
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment