Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Matomo 404 page tracking. #475

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Noam-Alum
Copy link
Contributor

@Noam-Alum Noam-Alum commented Aug 12, 2024

Added Matomo 404 page tracking.

This is referring to this issue.

This is a huge PR, I tried to overwrite the 404 page with no success and nothing on the web worked.
It seems like when you create the .vuepress/theme directory Vuepress is treating it like a whole new theme which lead me to copy the entire theme to this directory. (Believe me I read through their whole docs this does not work 😞, or anything alike for that matter)

Note that we are using an outdated version of Vuepress (1.9.10), I don't think anything would change.

Then I had to remove the script tag from the header tag in the .vuepress/config.js file since this script tag is on every page (including the 404 page), I cannot simply "Add the Matomo tracking code in this custom 404 page...before the line" since it's in the header and cannot be changed from the 404.vue layout file.

The solution I came up with is to use the needed JavaScript for pages in the Layout.vue file:

<template>
  <div
    class="theme-container"
    :class="pageClasses"
    @touchstart="onTouchStart"
    @touchend="onTouchEnd"
  >
    <Navbar
      v-if="shouldShowNavbar"
      @toggle-sidebar="toggleSidebar"
    />

    <div
      class="sidebar-mask"
      @click="toggleSidebar(false)"
    />

    <Sidebar
      :items="sidebarItems"
      @toggle-sidebar="toggleSidebar"
    >
      <template #top>
        <slot name="sidebar-top" />
      </template>
      <template #bottom>
        <slot name="sidebar-bottom" />
      </template>
    </Sidebar>

    <Home v-if="$page.frontmatter.home" />

    <Page
      v-else
      :sidebar-items="sidebarItems"
    >
      <template #top>
        <slot name="page-top" />
      </template>
      <template #bottom>
        <slot name="page-bottom" />
      </template>
    </Page>
  </div>
</template>

<script>
// Page tracking
var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
  var u="https://matomo.almalinux.org/";
	_paq.push(['setTrackerUrl', u+'matomo.php']);
	_paq.push(['setSiteId', '5']);
	var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
	g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();

import Home from '@theme/components/Home.vue'
import Navbar from '@theme/components/Navbar.vue'
import Page from '@theme/components/Page.vue'
import Sidebar from '@theme/components/Sidebar.vue'
import { resolveSidebarItems } from '../util'

export default {
  name: 'Layout',

  components: {
    Home,
    Page,
    Sidebar,
    Navbar
  },

  data () {
    return {
      isSidebarOpen: false
    }
  },

  computed: {
    shouldShowNavbar () {
      const { themeConfig } = this.$site
      const { frontmatter } = this.$page
      if (
        frontmatter.navbar === false
        || themeConfig.navbar === false) {
        return false
      }
      return (
        this.$title
        || themeConfig.logo
        || themeConfig.repo
        || themeConfig.nav
        || this.$themeLocaleConfig.nav
      )
    },

    shouldShowSidebar () {
      const { frontmatter } = this.$page
      return (
        !frontmatter.home
        && frontmatter.sidebar !== false
        && this.sidebarItems.length
      )
    },

    sidebarItems () {
      return resolveSidebarItems(
        this.$page,
        this.$page.regularPath,
        this.$site,
        this.$localePath
      )
    },

    pageClasses () {
      const userPageClass = this.$page.frontmatter.pageClass
      return [
        {
          'no-navbar': !this.shouldShowNavbar,
          'sidebar-open': this.isSidebarOpen,
          'no-sidebar': !this.shouldShowSidebar
        },
        userPageClass
      ]
    }
  },

  mounted () {
    this.$router.afterEach(() => {
      this.isSidebarOpen = false
    })
  },

  methods: {
    toggleSidebar (to) {
      this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen
      this.$emit('toggle-sidebar', this.isSidebarOpen)
    },

    // side swipe
    onTouchStart (e) {
      this.touchStart = {
        x: e.changedTouches[0].clientX,
        y: e.changedTouches[0].clientY
      }
    },

    onTouchEnd (e) {
      const dx = e.changedTouches[0].clientX - this.touchStart.x
      const dy = e.changedTouches[0].clientY - this.touchStart.y
      if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) {
        if (dx > 0 && this.touchStart.x <= 80) {
          this.toggleSidebar(true)
        } else {
          this.toggleSidebar(false)
        }
      }
    }
  }
}
</script>

And the needed JavaScript for 404s in the 404.vue file:

<template>
  <div class="theme-container">
    <div class="theme-default-content">
      <h1>404</h1>

      <blockquote>{{ getMsg() }}</blockquote>

      <RouterLink to="/">
        Take me home.
      </RouterLink>
    </div>
  </div>
</template>

<script>
// Page tracking
var _paq = window._paq = window._paq || [];
_paq.push(['setDocumentTitle',  '404/URL = ' +  encodeURIComponent(document.location.pathname+document.location.search) + ' /From = ' + encodeURIComponent(document.referrer)]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
  var u="https://matomo.almalinux.org/";
	_paq.push(['setTrackerUrl', u+'matomo.php']);
	_paq.push(['setSiteId', '5']);
	var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
	g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();

const msgs = [
  `There's nothing here.`,
  `How did we get here?`,
  `That's a Four-Oh-Four.`,
  `Looks like we've got some broken links.`
]

export default {
  methods: {
    getMsg () {
      return msgs[Math.floor(Math.random() * msgs.length)]
    }
  }
}
</script>



I proved that this works by generating the site and checking all of the js files (yes I went one by one) to see if they include the needed JavaScript:

404 page:

HTML tag:
AlmaLinux-404-HTML

JavaScript:
AlmaLinux-404-JS


Any page:

HTML tag:
AlmaLinux-Layout-HTML

JavaScript:
AlmaLinux-Layout-JS

All the other js files did not include any of the JavaScript manually added.




List of changes:

  1. Coppyed the whole default Vuepress theme to the .vuepress/theme directory.
  2. Removed the script tag from the header tag in the .vuepress/config.js file.
  3. Added JavaScript to track pages in the Layout,vue file.
  4. Added JavaScript to track pages in the 404.vue file.

I know these are huge changes, let me know what you guys think.

Maybe I missed something. 😄

@bennyvasquez
Copy link
Member

@codyro @jonathanspw @sboldyreva this seems sane to me, and seems to take all of the concerns from #467 into account. Do any of you see any problems here? The questions for me are around the automated deployments and the upgrades to vuepress, but I can't recall if we're even doing the second one regularly.

@bennyvasquez bennyvasquez added the enhancement New feature or request label Sep 2, 2024
@codyro
Copy link
Member

codyro commented Sep 4, 2024

@codyro @jonathanspw @sboldyreva this seems sane to me, and seems to take all of the concerns from #467 into account. Do any of you see any problems here? The questions for me are around the automated deployments and the upgrades to vuepress, but I can't recall if we're even doing the second one regularly.

I assigned #467 (the issue for this PR) to me to go over it once AlmaLinux/almalinux.org#619 is resolved as they're tangentially related. I'll aim to merge these both be EOW or sooner!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants