Skip to content

Commit 807bddb

Browse files
committed
Migrate to Google Analytics 4, GA4 as UA is deprecated
Main repo that this is forked from does not appear to be maintained. Referenced mmistakes/so-simple-theme#429
1 parent 779d3d8 commit 807bddb

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ google_fonts:
3030
weights: "400,400i,700,700i"
3131
- name: "Lora"
3232
weights: "400,400i,700,700i"
33-
google_analytics: UA-64050901-1
33+
google_analytics: G-R1L5NXLCYC
3434
disqus:
3535
shortname: leewc
3636
twitter:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% comment %}
2+
Old style Google analytics uses codes of the form UA-XXXXXXXX. The new Google Analytics 4 uses codes of the form G-XXXXXXX. Each has different javascript.
3+
{% endcomment %}
4+
5+
{%- if site.google_analytics contains 'UA-' -%}
6+
<script>
7+
if(!(window.doNotTrack === "1" || navigator.doNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.msDoNotTrack === "1")) {
8+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
9+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
10+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
11+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
12+
ga('create', '{{ site.google_analytics }}', 'auto');
13+
ga('send', 'pageview');
14+
}
15+
</script>
16+
{%- else %}
17+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics }}"></script>
18+
<script>
19+
window.dataLayer = window.dataLayer || [];
20+
function gtag(){dataLayer.push(arguments);}
21+
gtag('js', new Date());
22+
gtag('config', '{{ site.google_analytics }}');
23+
</script>
24+
{% endif %}

_includes/scripts.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{%- if site.footer_scripts -%}
2+
{%- for script in site.footer_scripts -%}
3+
{%- if script contains "://" -%}
4+
{%- capture script_path %}{{ script }}{% endcapture -%}
5+
{%- else -%}
6+
{%- capture script_path %}{{ script | absolute_url }}{% endcapture -%}
7+
{%- endif -%}
8+
<script src="{{ script_path }}"></script>
9+
{%- endfor -%}
10+
{%- else -%}
11+
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
12+
<script src="{{ '/assets/js/main.min.js' | relative_url }}"></script>
13+
<script src="https://use.fontawesome.com/releases/v5.0.12/js/all.js"></script>
14+
{%- endif -%}
15+
16+
{%- if jekyll.environment == 'production' and site.google_analytics -%}
17+
<!-- leewc: Override google-analytics from main theme given main theme is not maintained -->
18+
{% include google-analytics-custom.html %}
19+
{%- endif %}
20+
21+
{% if site.mathjax == true or site.mathjax.enable == true %}
22+
<!-- MathJax -->
23+
{% capture mathjaxjs %}https://cdn.jsdelivr.net/npm/mathjax@3/es5/{{ site.mathjax.combo | default: "tex-svg" }}.js{% endcapture %}
24+
<script>
25+
// http://docs.mathjax.org/en/latest/upgrading/v2.html
26+
MathJax = {
27+
tex: {
28+
tags: "{{ site.mathjax.tags | default: 'ams' }}" // eq numbering options: none, ams, all
29+
},
30+
options: {
31+
renderActions: {
32+
// for mathjax 3, handle <script "math/tex"> blocks inserted by kramdown
33+
find: [10, function (doc) {
34+
for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
35+
const display = !!node.type.match(/; *mode=display/);
36+
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
37+
const text = document.createTextNode('');
38+
node.parentNode.replaceChild(text, node);
39+
math.start = {node: text, delim: '', n: 0};
40+
math.end = {node: text, delim: '', n: 0};
41+
doc.math.push(math);
42+
}
43+
}, '']
44+
}
45+
}
46+
}
47+
</script>
48+
49+
<script type="text/javascript" id="MathJax-script" async src="{{ mathjaxjs }}"></script>
50+
{% endif %}
51+
52+
{%- if page.layout == "search" -%}
53+
{% include lunr-search-scripts.html %}
54+
{%- endif -%}

0 commit comments

Comments
 (0)