How to add a separate Analytics for each website page in Wordpress - wordpress

We have a WordPress installation that has locations as separate pages.
For example: mysite.com/colorado and mysite.com/alabama
We need separate google analytics for each of these as well as 1 for all of mysite.com.
Is there a way to do this with a WordPress plugin(s) or will we need to hand code some things?
Thanks in advance!

If I'm not mistaken, Google Analytics lets you view your analytics in a breakdown like that. That said, if you do need individual scripts, it would be relatively easy to program in. There may be some plugins that do this, but I'm not aware of any in particular, though a cursory glance showed plugins like Header and Footer Scripts that allow you to add scripts on a page by page basis.
Some themes also allow you to add SEO/Script settings per page/post. If that's the case, you can just open up each page and dump each script tag in the "header scripts" or similar section, and call it good. (Genesis is an example of a theme that does this).
If not, programming this would be relatively straight forward. I'd do it something like this:
add_action( 'wp_head', 'display_analytics_by_page', 1 );
function display_analytics_by_page(){
// Default Script Code with individual UA codes replaced
$script = '<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[UA-CODE]"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());
gtag(\'config\', \'[UA-CODE]\');
</script>';
// Array of UA codes by state
$codes = array(
'Alabama' => 'UA-123456789-1',
'Oregon' => 'UA-987654542-3',
'Vermont' => 'UA-000000000-0'
);
// Get title of this page
$title = get_the_title();
// If this page title exists in the codes array, swap placeholder and echo it.
if( isset($codes[$title]) ){
echo str_replace( '[UA-CODE]', $codes[$title], $script );
}
}
I commented along the way, but the gist is to put in the "default" script, but pull out the UA code. This will only work if you need the same exact script code in each one, otherwise you'll have to add each script to the $codes array instead.
Then create an array of the UA Codes (or full scripts if needed), keyed by the page title.
Then check the page title, and if that exists, pull that code in and echo it. This is run on the wp_head hook, so you just need to put this code in your functions.php (or similar) file.

If you want to go easy with no-coding, then go with a plugin. Jump to Plugins > Add New and Search for Google Analytics in the search box. Install the plugin named Google Analytics for WordPress (Formerly GADWP). Activate and connect the plugin with your Google Analytics property. When all is done, you will see a new tab beside your post's title. See a screenshot here.
And if you want the net analytics for the whole web site then head to your admin dashboard. A new widget will appear there with the analytics.
Documentation for the plugin can be found here.
I hope it helps.

I'm curious why you need a separate GA account for all the locations? Common practice in this scenario would be:
Use 1 account
Create 1 GA view for the entire domain
Create +1 view for each location by filtering traffic based on the URL
If the different accounts are related to limiting user access, know that you can grant user access based on each property view.

Related

Can I add my existing html homepage google analytics code into new wordpress homepage?

We are having a html website whose homepage and other few pages has google analytics code and keywords. Now we have created a new homepage on wordpress which will be our future homepage. So my question is can I use my existing homepage GA code into my new wordpress homepage? If yes then how? We don't want to lose our previous tracking data at any cost.
How can use it? Please Help.
TIA.
Yes, you can use your existing google analytics code by using WordPress plugins.
There are many plugins in WordPress which provides ability to integrate your google analytics code to your site and all pages dynamically and also display you a complete tracking reports as well.
here is one plugin link for your reference please check it.
https://wordpress.org/plugins/googleanalytics/
There are lot's of other plugins as well you can simply check out in WordPress plugins repository.
=> Also you can add your google analytics code programmatic as well.
Please copy below code and paste it into your activated themes functions.php file at the end of the file.
function add_google_analytics_code_to_pages(){
?>
<!-- Google Analytics -->
<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/analytics.js','ga');
ga('create', 'UA-ADD YOU GOOGLE ANALYTICS ID HERE', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<?php
}
add_action("wp_head","add_google_analytics_code_to_pages");
Just add your google analytics tracking id into about script.
Replace your tracking id instead of this code.
'UA-ADD YOU GOOGLE ANALYTICS ID HERE'
Unless I am missing something here, sure!
All you have to do is enter your Google Tracking code inside the <head> tags, within the header.php template file. Ideally, just before the closing </head> tag.
By inserting your Google Tracking code, within the header.php template file, every WordPress page will be tracked.
Your header.php template file will typically be found within the following directory path:
wp-content > themes > theme-name
If you are not the theme author, don't forget to create a Child Theme. You will then need to insert the appropriate header.php template file within said Child Theme.
Google Tag Assistance:
For the most part, the above should resolve your problem. You can check whether the Tracking code has been correctly installed by:
Reviewing the code within the Source Code
Installing Google Tag Assistance within Chrome
Exception:
Again, the above should be enough to resolve your issue. Some themes however, do create different headers, for different pages of the site. For example: An eCommerce site may want a different header on their shop pages to their Blog Pages. To achieve this, header template files, such as below would be created:
header-shop.php
header-blog.php
header-ppc.php
To then 'call' each header, the following example PHP entry would be placed within the relevant template file(s):
<?php get_header( 'shop' ); ?>
'shop' being whatever word appends the header- file name. In this case, 'shop', 'blog' and 'ppc'.
You do not need to worry much about this. All you need to do, is head into the root of your theme as follows:
wp-content > themes > theme-name
Look for any files with begin with header- and ensure they contain the Google Tracking code.
In the event you are using a Child Theme, simply copy the header- files over to the root of your Child Theme first and then modify accordingly.
Hope this helps.

Can I remove the JSON-LD schema that Yoast adds to my WordPress site?

I would like to remove the JSON-LD schema that Yoast applies to my WordPress site so that I can add my own. I have already added my own, and Google Structured Data Testing says that it is OK, but basically I have 3 separate JSON-LD schemas instead of two because of Yoast.
You can see what I mean here: https://search.google.com/structured-data/testing-tool/u/0/#url=http%3A%2F%2Fwww.yogabearpc.com
Yoast has added the WebSite schema and it seems unnecessary or even damaging?
I wanted to disable this because of the sitelinks searchbox and the fact that I don't have a search function that works globally, just on the blog. Having the search box enabled for me would have undesirable effects.
The easier option may just be to prevent Google using the sitelinks searchbox without having to touch the functions files. You can prevent Google using sitelinks searchbox on your site by using the following meta:
<meta name="google" content="nositelinkssearchbox" />
If you want to disable Yoast's JSON-LD all together then here's a snippet from my blog and the code I use on my site:
SOURCE
How to disable Yoast SEO Schema JSON-LD completely
function bybe_remove_yoast_json($data){
$data = array();
return $data;
}
add_filter('wpseo_json_ld_output', 'bybe_remove_yoast_json', 10, 1);
Login to your WordPress dashboard and head over to the editor
within the tab menu appearance, find your functions file (normally
named functions.php) and add the code below just before the PHP tag is
closed at the bottom.
Simplest way to completely disable the Yoast SEO schema JSON-LD
Add this line to functions.php file:
add_filter( 'wpseo_json_ld_output', '__return_empty_array' );
Source
If you want to disable just Organization or just Website, add this to your theme's functions.php file:
function bybe_remove_yoast_json($data){
if ( (isset($data['#type'])) && ($data['#type'] == 'Organization') ) {
$data = array();
}
return $data;
}
add_filter('wpseo_json_ld_output', 'bybe_remove_yoast_json', 10, 1);
Unless the data Yoast produces is wrong, there is no harm in having it. Quite the contrary, having more structured data is better than having less.
If having it is "unnecessary" depends on your definition of what is necessary. Some consumers might be interested in it, others not.
My guess is that Yoast adds a WebSite entity because of Google’s sitelinks searchbox rich snippet result, which allows Google users to search your site directly from the Google search result.

wordpress advanced custom fields google map api key

I have a problem with loading google map with the Advanced Custom Field plugin. I make everything like in the instruction on the plugin page here https://www.advancedcustomfields.com/resources/google-map.
I add google-map field in ACF, but on the page where it should be it appears for a second, and then disappears with the inscription "Oops! Something went wrong. This page didn't load Google Maps correctly. See the JavaScript console for technical details." (see the screenshot). Console says that I need to set the Google API key. I guess I also need to modify some strings in .js file from the ACF instruction, but I don't know which ones. May be someone could help.
Thank you in advance.
screenshot
ACF updated the Google Map documentation
You first have to get a Maps API key and make sure you activate the following APIs :
Maps JavaScript API
Geocoding API
Places API
Then register the API key in your functions.php
If using ACF free
function my_acf_google_map_api( $api ){
$api['key'] = 'xxx';
return $api;
}
add_filter('acf/fields/google_map/api', 'my_acf_google_map_api');
If using ACF pro
function my_acf_init() {
acf_update_setting('google_api_key', 'xxx');
}
add_action('acf/init', 'my_acf_init');
In my case I had to delete & recreate the field so that it saves correctly.
A solution could be editing the functions.php in your template
//TODO: fix api key for advanced custom field
add_action('acf/fields/google_map/api', function($api){
$api['key'] = '<YOUR_API_KEY>';
return $api;
});
or you can check my article for a complete solution
add this line in your script..replace with your key..
<script src="javascripts/jquery.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR-API-KEY"></script>
<script type="text/javascript" src="javascripts/jquery.googlemap.js"></script>
I have found a couple of different solutions for this issue, but before starting to explain what to do let me remember to you to get a google maps api key.
I followed these instructions because I'm using Listify theme, but I'm pretty sure they can help you regardless the theme you have.
Here my solutions:
frontend
Somewhere (I guess in your functions.php or in your-awesome-widget.php) you should have a line like these
wp_enqueue_script( 'googlemaps_api' );
or
wp_enqueue_script( 'googlemaps' );
the solution I've found is add the key in the script registration before enqueuing it, in this way
wp_register_script('googlemaps', 'http://maps.googleapis.com/maps/api/js?key='.$YOUR_API_KEY, false, '3');
wp_enqueue_script('googlemaps');
Backend
This one is quick and totally dirty because I've read that ACF support is already working on the official solution, so, for me, is not a problem if it will be erased by a plugin update.
Open those two files:
advanced-custom-fields/js/input.min.js
advanced-custom-fields/js/input.js (in theory if you are using the .min version this one is useless)
This piece of code is repeated twice in each file:
google.load('maps', '3', { other_params: $.param(self.api), callback: function(){ ...
change those two occurencies adding the key as querystring, in this way
other_params: $.param(i.api) + 'key=YOUR_API_KEY', callback ...
Et voilá! It should work.
The official page about the topic is here
I hope to have been helpful!
With the current version (4.4) of ACF, you can find functions.php in the template you are using and add this to the end of the code:
function my_acf_google_map_api( $api ){
$api['key'] = 'YOUR_API_KEY';
return $api;
}
add_filter('acf/fields/google_map/api', 'my_acf_google_map_api');
Change 'YOUR_API_KEY' to your API KEY generated from Google.

Create a Dynamic Site Address in a Wordpress Menu

I'm doing some work on an existing site that is based on the Wordpress theme, but uses about 15 plugins (Including Buddypress). One in particular is the WP Sliding Login|Dashboard plugin, which has a link to the user's activity feed. I found the code that creates that link in the wp-sliding-login-dashboard.php file:
<?php
if ( is_user_logged_in() ) {
global $current_user;
$username = $current_user->user_login;
echo ' <li>Activity Feed</li>';
}
?>
I want to use this code to send the user to the same location, but using the a link at the top of the home page. Unfortunately, the home page links are all created using Wordpress menus, which as far as I can tell, only allow for the use of static links attached to existing pages.
Do I create a dummy page to link to that exits only to execute the above code? Is that even possible? Picture a five-year-old trying to read Shakespeare, and you have an idea of my ability as a coder, so feel free to engage me as such - i.e. if you say, "oh just create a scoping function instead of creating a global function", i would stare at you drooling and confused.
Images for clarity: The sliding login menu (WP-Sliding Login|Dashboard Plugin), showing the target URL in the status bar as www.ferrignofit.com/members/FerrignoFit/activity/ (the current logged in user is FerrignoFit):
http://i.imgur.com/NPvmCXU.jpg
The main page Wordpress-based menu, which i want to go to the above URL, but is currently going to www.ferrignofit.com/activity/, a different page:
http://i.imgur.com/dIiFpDC.jpg
So here's a jQuery solution for this specific issue. From seeing the images you have, what you want to do is target a specific anchor in your dynamic WordPress menu.
As you may be aware, you can create custom links for the WordPress menu function... it simply lets you set a label and a URL. You should create such item and just give it a hash for the URL for now.
Now set a class for that specific menu item so you can have a nice handle for jQuery to target it (otherwise you can use the dynamic class that WordPress creates for each specific menu-item).
One you have a class set or you know what you need to target then you can add this block of code before your menu.
<?php if (is_user_logged_in()){ ?>
<script>
$(document).ready(function(e) {
var targetNav = $('li.customClassName a');
var userName = '<?php $current_user = wp_get_current_user(); echo $current_user->display_name;?>';
var userUrl = 'http://www.mywebsitename.com/members/'+ userName +'/activity/';
targetNav.attr('href',userUrl);
});
</script>
<?php } else { ?>
<script>
$(document).ready(function(e) {
var targetNav = $('li.customClassName a');
targetNav.attr('href','http://www.stackoverflow.com');
});
</script>
<?php } ?>
Please not that I am using PHP to get the current username in WordPress, after I get the username and I store it in the userName variable I use it in the userUrl to set it with the path that I want.
On another note, I'm using is_user_logged_in() so you have the option of making the link something else if the user is in fact not logged in. So with this if statement one of the two blocks of code will be output by PHP.
As soon as my two variables are set, I simply target my menu item with jQuery and modify the href attribute to replace the hash with the path and dynamic username.
Though this is not very good practice to mix JS and PHP, I'd like to learn myself here what other solutions someone can suggest so I'm posting this as a solution for your very specific issue.
I tested this code with my own WordPress site and it works, just remember that this code NEEDS to be in a PHP file otherwise the PHP used in the <script> tags won't mean anything to the server if it's in a .js file.

Wordpress Sign-ups Using GA Goal Tracking

I'm trying to use GA Tracking to Track Wordpress Sign-ups on my site. I'm assuming I need to use Goal Tracking and NOT Event tracking for this. I tried setting up goal tracking but it's not working. I am not using any GA plugins like Yoast, etc.
I can't get this to work with Wordpress:
I set my Goal URL to: /wp-login.php?checkemail=registered (URL landing page for sign-ups)
Match Type: "Exact Match"
No Goal Funnel was set.
GA is not tracking any of my sign-ups. What I am doing wrong here? My site is www.StreetofWalls.com if you would like to test.
The script that loads google-analytics isn't on the login page, so you're not getting any of the /wp-login.php?checkemail=registered pageviews sent back to google.
Put this on your wp-login.php page
<script type='text/javascript' src='http://www.streetofwalls.com/wp-content/themes/sow3/js/google-analytics.js'></script>
While TomFuertes's answer will work, it is not good to modify core WordPress files like wp-login.php. Updates to WordPress will overwrite any changes that you make and avoiding WordPress updates is a very bad idea.
I recommend adding the code using the login_head hook in your functions.php file:
function add_ga_to_login_page(){
echo '<script>';
echo 'GA TRACKING CODE HERE';
echo '</script>';
}
add_action('login_head', 'add_ga_to_login_page');

Resources