znielsen
znielsen

Reputation: 93

Getting Google analytics working with Pelican

So I recently started blogging with Pelican, and everything is going great with the exception of Google Analytics. I publish my blog using the

make s3_upload

command, which uses the publishconf.py file.

To get my Tracking ID, all I did was copy paste my Tracking ID from the google analytics page into the Google Analytics line in the publishconf.py file, like so

# Following items are often useful when publishing

#DISQUS_SITENAME = ""
GOOGLE_ANALYTICS = "UA-########-#"

Any help would be greatly appreciated, I have been racking my brain trying to solve the problem.

Upvotes: 6

Views: 1955

Answers (2)

getup8
getup8

Reputation: 8238

If the Analytics snippet is not in the output for your site, it's likely the theme you're using didn't include the proper code to include it.

Each theme should have a base.html file, you should look at that to make sure it's included there. If not you can add something like:

{% if GOOGLE_ANALYTICS %}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{GOOGLE_ANALYTICS}}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '{{GOOGLE_ANALYTICS}}');
</script>
{% endif %}

Upvotes: 3

Jeff
Jeff

Reputation: 69

Solution

  1. editing publishconf.py

    GOOGLE_ANALYTICS = "UA-########-#"

  2. Building the website

    pelican content

  3. Modifying the website according to settings = adding google analytics

    pelican -s publishconf.py .

  4. Deploying

    cd output && git push origin master

Tests that the steps work:

  1. Locally at the end of output/index.html it added:

    <script type="text/javascript">
    (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-125105451-1', 'auto');
    ga('send', 'pageview');
    </script>
    
  2. Check that the javascript code has been addded to your github repo. Check that the code is serve in a " private navigation " ( in order to disable browser caching). Right click on your webpage > see source.

Context: Deployment to github pages

Git for the blog:

cd output/ 
git init .
git remote add origin [email protected]:<username>/<username>.github.io.git
# Deploy with:
# git add . && git commit -m "Commit description" && git push origin master

Git for the blog engine:

cd output && cd .. # ensure to be in the good repository
git init .
git submodule add [email protected]:<username>/<username>.github.io.git output/

Upvotes: 2

Related Questions