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>
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 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.
i'm using the Contact form 7 Redirect, when a new user signs up.
When the user signs up a message is displayed, but the redirect happens instant, so there is no time to read the message.
Is there a way to delay the redirect with Javascipt?
Deactivate the plugin that you're using for form submission redirect.
put it on functions.php
add_action( 'wp_footer', 'prefix_my_footer_scripts' );
function prefix_my_footer_scripts(){
?>
<script>
document.addEventListener( 'wpcf7mailsent', setTimeout(function( event ) {
location = 'http://example.com/';
}, false ), 1000); // Replace location and 1000(ms) as your wish
</script>
<?php
}
see more
The accepted answer did not work for me. It fired on page load. So i wrote it like this:
add_action( 'wp_footer', 'load_footer_scripts' );
function load_footer_scripts(){
?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
setTimeout(function( event ) {
location = 'http://example.com/';
}, 4500 );
}, false );
</script>
<?php
}
You can use Contact Form 7's Custom DOM Event to achieve this. You need to replace the http://example.com/ in the code to the URL you want to redirect to.
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'http://example.com/';
}, false );
</script>
First, I want to say, I am new in code. Recently I find the Ecommerce Tracking is not working, of course missing Ecommerce Tracking code. I search a lot from Internet, still cannot figure out, who can help me, thanks a lot.
I find the follow 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','https://www.google-analytics.com/gtag.js','ga');
ga('create', 'UA-88888888-1', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '1234', // Transaction ID. Required.
'affiliation': 'Fresh Egg', // Affiliation or store name.
'revenue': '43.66', // Grand Total.
'shipping': '3', // Shipping.
'tax': '1.29' // Tax.
});
ga('ecommerce:addItem', {
'id': '1234', // Transaction ID. Required.
'name': 'Fresh Egg', // Product name. Required.
'sku': 'AB12345', // SKU/code.
'category': 'Blue eggs', // Category or variation.
'price': '22.33', // Unit price.
'quantity': '1' // Quantity.
});
ga('ecommerce:send');
</script>
I donot know how to change the date, like, what "1234" means? Is this a certain date or something in from the code? And others! I know this may be a very foolish question, but I really need your help! Thanks a lot!
First of all, make sure ecommerce plugin is enough for what you need or maybe you should use enhanced ecommerce plugin.
You need to change, dynamically, each value depending on the transaction that is being executed.
It will depend on the programming language your site uses. For example, in php your code could be something like this:
ga('ecommerce:addItem', {
'id': '<?php echo $transacion_id; ?>',
'name': '<?php echo $product_name; ?>',
'sku': '<?php echo $sku; ?>',
'category': '<?php echo $product_category; ?>',
'price': '<?php echo $product_price; ?>',
'quantity': '<?php echo $product_quantity; ?>'
});
Consider using Google Tag Manager to implement your Analytics Ecommerce
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>