My Google Analytics setup for hugo is not working - google-analytics

I've added my tracking id to my config.toml and everything looks like its matching up. Yet I haven’t seen any realtime data come in when I launch https://analytics.google.com/
theme = "osprey-delight"
tags = ["personal", "homepage", "portfolio"]
# canonifyURLS = true
googleAnalytics = "G-SYJ123456"
disableKinds = ["taxonomy", "taxonomyTerm"] # This theme does not currently use "tag" and "category" taxonomies
enableEmoji = true
pygmentsUseClasses = true
I'm using a theme(osprey-delight) and in the layouts/partials directory, theres a head.html that contains template string, will paste below:
{{ if .Site.GoogleAnalytics }}
{{ template "_internal/google_analytics_async.html" . }}
{{ end }}
When i deploy to firebase, i do see a script tag for ga in my main index.html file in /public directory, here it is below -
<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'G-12345689', 'auto');
ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
What am I missing? I feel like I've don't everything right, but no luck yet.

Update February 2020
Below you can find the solution with Universal Analytics, alternatively you can try implementing the solution for Hugo to send data to your GA4 data stream: https://gist.github.com/zjeaton/42246742cdaf2fb46400d04c2eba9a8a#file-analytics-gtag-html
Solution with Universal Analytics
I suggest you create a Universal Analytics Property (as shown in following image) and use the relative identifier UA-XXXXXXX-X (easy way):
Alternatively, if you want to use GA4 Property you have to follow relative syntax:
https://developers.google.com/analytics/devguides/collection/ga4

Instead of creating a Universal Analytics Property, you can implement the solution for Hugo as proposed by Zachary Eaton and keep your GA4 property: https://gist.github.com/zjeaton/42246742cdaf2fb46400d04c2eba9a8a#file-analytics-gtag-html

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.

How to send traidtional GA event in GTM's custom HTML tag

I'm using GTM in my company's online shop, and I installed some other tag via GTM's custom HTML tag, like this:
<script>
//some JS code...
var customParam=[{"key":"value"}];
var jsElm=document.createElement("script");
jsElm.type="text/javascript";
jsElm.async=true;
jsElm.src="https://some_tags.com/tag.js?s=CV";
document.body.appendChild(jsElm);
if(today < some date)
{
SEND GA EVENT here.....
}
</script>
The traditional GA event script
ga('send', 'event', 'XXX', 'pageview', 'some description', 1);
isn't working anymore here.
How do I did this ?
And I heard someone said that I should put data into DataLayer and do some bla bla bla... but I was understanding that too few to try. Is that a right way ?
I am going to answer with another question. Why are you sending events through HTML tags instead of Google Analytics Tag that are build just for that purpose?
I ll work with the assumption that you are new with this tool but the idea is that GTM replaces all old GA code on site.
Just create a new Tag and select 'Universal Analytics' and after that you can choose which kind of information you want on Google Analytics.
Its pretty self explanatory as a tool so i strongly recommend you to forget about those old GA codes and work with what you got.
In addition to this have in mind that you can use custom javascript variables to scrap DOM values in case you need to.
Here is our guru Simo Ahava: https://www.simoahava.com/
If you need anything else just ask.

Google Analytics User ID view empty

I have 2 websites running on 2 separate servers.
The "public" one is www.projacked.com
The "members only" one is global.projacked.com
I enabled cross domain tracking using the linker plugin.
In the internal page I have the following (it's twig on Symfony):
ga('create', 'UA-XXXXXXXX-X', 'auto', {allowLinker: true});
ga('send', 'pageview');
{% if app.user is not null %}
ga('set', 'userId', '{{ app.user.id ~ '-' ~ app.user.username }}');
{% endif %}
// Loads the Linker plugin
ga('require', 'linker');
// Instructs the Linker plugin to automatically add linker parameters
// to all links and forms pointing to the domain "projacked.com".
ga('linker:autoLink', ['projacked.com'], false, true);
As an example of how the line that sets the user id gets rendered by the template we have:
ga('set', 'userId', '1201056-justintrudeau');
This line does not report any error in javascript, and I tested that it gets called.
I created a separate view for this. I tried as website to track either www.projacked.com or global.projacked.com. I've enabled user id for this view. In any case I cannot see any user on this view.
This is the configuration for the view:
I've checked that the snipped is with the same UA-XXXXXXXX-X (of course my code).
Questions:
shall I use the members-only website in the view, as it's the only one that can set the userID?
do I have to make any change in property? There I have only one website (www.projacked.com). Do I have to do anything to make it register stuff also related to the other website (global.projacked.com), apart from adding the same UA-XXXXXXXX-X there too?
why on hell I don't see the users on the members-only site?
Thank you
From your code it looks like you are setting the userId after your pageview call, so it is never actually sent to the server. Move it upwards and things should start to work ("set" calls are not hoisted, they are applied only to hits that follow the "set").
Also the user id and name might be personally identifiable information and would need to be hashed with at least SHA256 before your are allowed to store it within Google Analytics.

Decoding a base64 url in Google Analytics

I currently have landing pages taking the following shape :
http://rootdomain/index.html?ref=[base64encoded-ref]&text=[base64encoded-text]
When I watch my stats in google analytics, I have urls looking like
http://rootdomain/index.html?ref=c3RhY2tvdmVyZmxvdw==&text=bWFyd2Fubg==
but I'd like to see
http://rootdomain/?index.html?ref=stackoverflow&text=marwann for example.
Any idea how to achieve this?
Oki so you can do this, but it can be tricky, so be careful.
Without GTM:
So on the page with Universal analytics, you can actually tell what must be the value that will be recorded. you can decode your path using JS and pass it like below:
ga('send', 'pageview', { 'page': '/YOUR-CUSTOM-PATH' });
Using GTM:
Changing the "dp" value can be accomplished in GTM by setting the desired value in the page field, under "Fields to set":
Let me know if you have doubts.

GTM/UA Track multiple authors

Seems like this would be quite simple but i cannot find much documentation available. Currently on our article system, each author name appears like this on each page:
<div id="author-info">
Mr. Man
According to Google, using custom dimensions is the way to do track authors in UA. Setup a custom dimension "Author" and set the scope to Hit/Session/User. Not sure i fully understand the different scope options. I get the following code:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);
Which i'm assuming should be:
var author-id = 'Mr. Man';
ga('set', 'dimension1', author-id);
Is that correct?
Also, how can i pass the author information into the dataLayer and then call the author page views through GTM? How about multiple authors?
Ok, this answer comes from the G+ analytics community (link below). Credit goes to https://plus.google.com/u/0/+SimoAhava
Using GTM, we can do the whole thing using macros and set fields:
Create a Custom JavaScript Macro which returns the author name:
function() {
try{
return document.getElementById("author-info").children[0].text;
} catch(e) {
return "";
}
}
In your Pageview tag, go to More Settings -> Custom Dimensions, add custom dimension, put the index number of the CD you created in the required field and add the JavaScript macro to the Dimension field.
This setup grabs the text content of the page's author link and sends it as a custom dimension with the pageview. Since it's hit-scope, every pageview will be attached with the author info of the page.
https://plus.google.com/104258622890980927916/posts/e6cC6y8q1m6

Resources