I am a beginner with wordpress and I want to ask somebody to help me. I would like to track page 404. I have 2 analytics codes. One of them is displayed on every page. The other is for displaying only on the 404 pages. I don't know where and how to put the second analytics code which is for page 404 only. I ask for an advice :)
The code is below:
<?php if (is_404()) { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview', '/404.html?page='+document.location.pathname + document.location.search + '&from=' + document.referrer);
</script>
<?php } else { ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-X');
</script>
<?php } ?>
Thank you in advance!
You can use the following, it will need to go inside of the <head> which will be located in the header.php file of your active theme:
<?php if(is_404()) { ?>
Paste 404 only tracking here
<?php } else { ?>
Paste normal tracking here
<?php } ?>
You'll need to add both of your tracking codes where I've labelled but that will show them on the relevant pages.
Related
I have a WordPress website and a registration system in it. Lets say I have the following Google Ad example:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>
<!-- Homepage Leaderboard -->
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1234567890123456"
data-ad-slot="1234567890"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
How can I display the Ad only to guest users? I've been stuck with this for days. Is there a plugin? or can I edit the user roles somewhere?
<?php if( false === is_user_logged_in() ) { ?>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>
<!-- Homepage Leaderboard -->
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1234567890123456"
data-ad-slot="1234567890"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php } // endif ?>
I have the below code in my header:-
if ($_SESSION['lang'] == 'us') { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-2', 'auto');
ga('send', 'pageview');
</script>
<?php } else { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
</script>
<?php } ?>
So depending on which language of the site you are using, a different UA code is used.
It's all working but on Google Analytics I get the following error message:-
Tracking Code Mismatch
Tracking code on page www.xxxxxxxxxx.com does not match property XXXX Website's tracking ID UA-xxxxxxxxx-1.
Any ideas how I can sort this?
Thanks to #GreatBlakes - this has resolved the problem:-
//Analytics tracking for US
$language = $_SESSION['lang'];
if ($language == 'us') {
$UA_CODE = 'UA-XXXXXXXX-2'; //USA
} else {
$UA_CODE = 'UA-XXXXXXXX-1'; //UK
}
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', <?php echo $UA_CODE; ?>, 'auto');
ga('send', 'pageview');
</script>
I'm having a little trouble with getting author tracking set up on my google analytics. This is my code
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-XX', 'auto');
ga('send', 'pageview', {
'dimension1': '<?=$author?>'
});
</script>
Now, when I look at the data in my Google Analytics page, I see the author name as "" and not the actual authors
The loop
Are you sure you set it up in the wordpress loop? It really depends, where the variable is setted. If you have this at the beginning oif HTML page, the variable do not have to be available in this time.
Shorthand syntax
It seems to be correct shorthand <?=$author?>. But try full syntax <?php echo $author; ?>.
This should work fine:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-1', 'auto');
<?php if (is_single()) :
$post_author_id = get_post_field( 'post_author', $post_id ) ?>
ga('set', 'dimension1', '<?php echo get_the_author_meta('display_name', $post_author_id); ?>' );
<?php endif ?>
ga('send', 'pageview');
</script>
Anyone have simple method for one pager wordpress site? I mean redirecting all pages to frontpage. I dont want to delete all template files and meta nofollow is too simple. Any ideas, plugins, htaccess tricks? Thanks
Write this in your functions.php:-
add_action('wp_head', 'my_page_redirect', PHP_INT_MAX);
function my_page_redirect(){
if( !is_front_page() ) {
?>
<script type='text/javascript'>
setTimeout(function() {
document.location = "<?php echo site_url(); ?>"; }, 500
);
</script>
<?php
}
}
iam trying to control supersized slideshow with this code
<script type="text/javascript">
function ToggleSlideShow(){
api.playToggle();
}
</script>
</head>
<body>
//Check frontpage
<?php if (is_front_page() ) { ?>
//not frontpage
<?php } else { ?>
//call script and pause slideshow!
<script type="text/javascript">
ToggleSlideShow();
</script>
<?php } ?>
But it dosnt work, can anyone please help?
I gave up :-) instead i just loaded a different header using the if_front_page()