fatiando / community

Community resources, guidelines, meeting notes, authorship policy, maintenance, etc.
Other
8 stars 5 forks source link

Ditch Google Analytics and use Plausible #46

Closed santisoler closed 2 years ago

santisoler commented 2 years ago

Description:

On the Development Call of 2022-01-14 we decided to stop using Google Analytics and replace it for Plausible, an open-source lightweight and privacy-friendly alternative.

We should replace the existing Google Analytics script:

  <!-- Google Analytics tracking 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/analytics.js','ga');

    ga('create', 'UA-38125837-1', 'auto', {'storage': 'none'});
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
  </script>

For this one:

  <!-- Plausible analytics for anonymous usage statistics -->
  <script defer data-domain="fatiando.org" src="https://plausible.io/js/plausible.js"></script>

This should be done through out the websites of the project, including:

Apply to:

Here I list all the repos that should be modified. For each one of the packages repos, check the box only after the snippet has been replaced in the main branch and in the older docs as well.

Further instructions:

We want your help!

We know that maintenance tasks are very demanding, so we don't expect a single person to tackle this issue by themselves. Any help is very welcomed, so please comment below that you want to take care of the changes on any repository and we will assign it to you.

Update

Considering the documentation pages of the older versions of each package we can count +200 HTML files that need to be modified. So it's better to perform this change automatically.

I wrote a simple Python script to apply the modifications to a single HTML file:

plausible.py:

#!/usr/bin/env python
"""
Replace Google Analytics snippet for the one for Plausible
"""

import sys

domain = "fatiando.org"
plausible_lines = [
    "<!-- Plausible analytics for anonymous usage statistics -->\n",
    f'<script defer data-domain="{domain}" src="https://plausible.io/js/plausible.js"></script>\n',
]

# Get filename from the first argument
filename = sys.argv[1]

# Read html file
content = []
in_script = False
with open(filename, "r") as f:
    for line in f:
        if "Google Analytics" in line:
            in_script = True
            # Get number of indentation spaces
            n_spaces = 0
            for n_spaces, char in enumerate(line):
                if char != " ":
                    break
                n_spaces += 1
            # Append the plausible lines to the content list
            for plausible in plausible_lines:
                content.append(" " * n_spaces + plausible)
        if in_script and "</script>" in line:
            in_script = False
            continue
        if not in_script:
            content.append(line)

# Overwrite file
with open(filename, "w") as f:
    f.write("".join(content))

To use it, copy and paste the code to a file, give it execution permissions with chmod u+x plausible.py and run it by passing the html file that you want to change as the first argument:

./plausible.py index.html

In order to run this script over multiple files, we can use the following bash line:

for file in **/*.html; do ./plausible.py $file; done

Check if the changes have been successfully applied by running git diff.

We can change the domain from fatiando.org to something like legacy.fatiando.org by changing the domain variable in the plausible.py script.

santisoler commented 2 years ago

Should we add a small sentence on the footers with a privacy disclaimer about why we are using Plausible and what is it tracking?

leouieda commented 2 years ago

I think it’s probably better not to do that. Since if we ever need to change providers it will be yet another thing to update.

leouieda commented 2 years ago

Plausible doesn’t support joining multiple subdomains into a single entry so the legacy docs need to use:

<script defer data-domain="legacy.fatiando.org" src="https://plausible.io/js/plausible.js"></script>

The stats will be separate but we can always join them when exporting the data.

santisoler commented 2 years ago

I've just updated the PR description comment by adding a script to replace google analytics for plausible automatically. ☝🏼

santisoler commented 2 years ago

We are done! Plausible is working throughout the project, no trackers left. I'm closing this.