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>
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.
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
After switching to storefront theme leaflet map has disappeared.
link how it looks
functions.php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
/* wp_enqueue_style( 'bootstrap', 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' ); */
wp_enqueue_style( 'leaflet.css','http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css');
}
function theme_scripts(){
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js', array('jquery'), '1.12.2', false);
wp_enqueue_script('bootstrap-js', 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js', array('jquery'), false);
wp_enqueue_script('leaflet.js', 'http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js' ,false);
wp_enqueue_script('main.js', get_template_directory_uri(). '/js/main.js');
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
Bootstrap is commented because it breaks website view (it becomes smaller). But without commenting map is not working nevertheless.
I have added link to the map page. I dot not know is it proper way.
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri() ; ?>/css/bootstrap.min.css">
<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri() ; ?>/js/main.js"></script>
You must have following javascript instruction to create a map
var map = L.map('mapid').setView([latitude, longitude], zoom);
See documentation
If you are using a Wordpress plugin, you should refer to its documentation
A look into the console would have been useful:
TypeError: $ is not a function
Change in main.js this line:
$( document ).ready(function() {
to
jQuery( document ).ready(function($) {
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>