Skip to content

Commit 70900d2

Browse files
authored
feat: tag maintainer visits, track code copies and add social cards (#83)
Follow-up to a review of this site's Google Analytics and Search Console data. Over the last 90 days roughly 55% of users came from data-center cities with 0 s engagement. Maintainer visits were about 21% of engaged sessions and half of the `click_github` key events. Nothing recorded that a reader copied a workflow or hook. Links shared in chats had no preview card, and crawlers asking for `/robots.txt` landed on the 404 page. This PR only changes the site. The Google Analytics side (turning the Internal Traffic filter on, key events, custom dimensions) and Search Console are handled separately. ## Before / After (改动前 / 改动后) None of these changes alters a page's layout. The only new visual is the social card, shown below. ### Behaviour | | Before (live site, `origin/main`) | After (this branch) | |---|---|---| | Maintainer visits | Counted like any other visit | `?internal=1` stores a flag in `localStorage`; from then on every event carries `traffic_type=internal` (sent as `tt=internal`). `?internal=0` removes it. The project docs under `/cpp-linter-action/` etc. read the same flag. | | Copying a code block | No event | `copy_code` event with `snippet` (section id, or `cl-workflow` for the home page block) and `code_title` (the file name above the block) | | Open Graph / Twitter tags | 0 on every page | 13 on every page, with a 1200×630 card | | 404 page `<title>` | `cpp-linter` (same as a real page) | `Page not found - cpp-linter` | | `/robots.txt` | 404 (served the 404 page, which was then counted as a page view) | 200, `Allow: /` plus the sitemaps of this site and the four project docs | | Home page `<title>` | `C/C++ Linting - cpp-linter` | `C/C++ Linter with clang-format and clang-tidy - cpp-linter` | ### Old URLs that Google Analytics recorded as 404s `hooks/redirects.py` writes a small redirect page for each one after the build. Each page has a meta refresh, `location.replace`, a canonical link and `noindex`. | Path | Before | After | |---|---|---| | `/docs/` | 404 | → `/getting-started/` | | `/merch/` | 404 | → `/sponsor/#t-shirt-perk` | | `/inputs-outputs/`, `/examples/`, `/permissions/`, `/pr-review-caveats/` | 404 | → the same page under `/cpp-linter-action/` | | `/cli_args` | 404 | → `/cpp-linter/cli_args.html` | | `/cpp_linter_rs/`, `/cpp_linter_rs/cli.html` | 404 | → `/cpp-linter-rs/`, `/cpp-linter-rs/cli/` | ### Evidence: events sent by the real gtag.js Built this branch with the measurement ID swapped for a fake one (`G-TESTTEST00`), so nothing reached the real property. I served it locally and recorded every `g/collect` request in a browser while going through the pages in this order: | # | Page opened (in order) | Event | `tt` (traffic_type) | `ep.snippet` | `ep.code_title` | |---|---|---|---|---|---| | 1 | `/getting-started/` (no flag) | page_view | — | | | | 2 | same page, copy button in "On every pull request" | copy_code | — | `on-every-pull-request` | `.github/workflows/cpp-linter.yml` | | 3 | `/getting-started/?internal=1` | page_view | `internal` | | | | 4 | same page, copy button in "Before every commit" | copy_code | `internal` | `before-every-commit` | `.pre-commit-config.yaml` | | 5 | `/` (no parameter, flag still stored) | page_view | `internal` | | | | 6 | home page "Copy" button | copy_code | `internal` | `cl-workflow` | `.github/workflows/cpp-linter.yml` | | 7 | `/blog/?internal=0` | page_view | — | | | | 8 | `/blog/` | page_view | — | | | ### Evidence: built `<head>` of the home page Before (live): ```html <title>C/C++ Linting - cpp-linter</title> <!-- no og:* or twitter:* tags --> ``` After (`mkdocs build --strict`): ```html <title>C/C++ Linter with clang-format and clang-tidy - cpp-linter</title> <meta property="og:type" content="website"> <meta property="og:site_name" content="cpp-linter"> <meta property="og:title" content="C/C++ Linter with clang-format and clang-tidy"> <meta property="og:description" content="clang-format and clang-tidy for C and C++ pull requests, pre-commit hooks and CI, with the LLVM version you pin."> <meta property="og:url" content="https://cpp-linter.github.io/"> <meta property="og:image" content="https://cpp-linter.github.io/static/og-image.png"> <meta name="twitter:card" content="summary_large_image"> ``` ### Social card Before: no card. After: ![Social card](https://raw.githubusercontent.com/cpp-linter/cpp-linter.github.io/pr-assets/feature-analytics-and-seo/og-card-after.png) ## Notes - The flag snippet runs in the `analytics` block, before Material's own snippet. That way `gtag('set', {traffic_type: 'internal'})` is queued ahead of the first `page_view`, which rows 3 and 5 above confirm. - To report on `snippet` and `code_title`, they need to be registered as event-scoped custom dimensions in Google Analytics. That happens outside this PR. - `robots.txt` lists `/cpp-linter/sitemap.xml`, which is added by cpp-linter/cpp-linter#237. Until that one is deployed, that URL returns 404 to crawlers, which is harmless. - Companion PRs add the same Google Analytics property and the same flag to the project docs: cpp-linter/cpp-linter-action#504, cpp-linter/clang-tools-pip#229, cpp-linter/cpp-linter-rs#502 and cpp-linter/cpp-linter#237. ## After merging Open `https://cpp-linter.github.io/?internal=1` once on each device and browser you use. To check it is working, wait about a day, open any GA4 report, add the comparison "Test data filter name = Internal Traffic" (it exists while the filter is in Testing), and confirm your own visits show up there. Only then should the filter be switched to Active.
1 parent 2ac64d5 commit 70900d2

7 files changed

Lines changed: 186 additions & 1 deletion

File tree

‎docs/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hide:
44
- toc
55
- feedback
66
template: home.html
7-
title: C/C++ Linting
7+
title: C/C++ Linter with clang-format and clang-tidy
88
description: clang-format and clang-tidy for C and C++ pull requests, pre-commit hooks and CI, with the LLVM version you pin.
99
---
1010

‎docs/overrides/404.html‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "main.html" %}
2+
3+
{#-
4+
Gives the 404 page a title of its own. With Material's default it was titled
5+
"cpp-linter" like a real page, so missing pages were hard to tell apart in
6+
Google Analytics.
7+
-#}
8+
9+
{% block htmltitle %}
10+
<title>Page not found - {{ config.site_name }}</title>
11+
{% endblock %}
12+
13+
{% block content %}
14+
<h1>404 - Not found</h1>
15+
{% endblock %}

‎docs/overrides/main.html‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,112 @@
11
{% extends "base.html" %}
22

3+
{#-
4+
Visits from the maintainers are tagged so that the Internal Traffic data
5+
filter in Google Analytics can drop them. Opening any page with ?internal=1
6+
stores a flag in localStorage, ?internal=0 removes it, and while the flag is
7+
there every event carries traffic_type=internal. The docs under
8+
/cpp-linter-action/, /clang-tools-pip/, /cpp-linter-rs/ and /cpp-linter/ are
9+
on the same origin and read the same flag. It runs before Material's own
10+
snippet, so the setting is queued ahead of the first page_view.
11+
-#}
12+
13+
{% block analytics %}
14+
{% if config.extra.analytics %}
15+
<script>
16+
try {
17+
var param = new URLSearchParams(location.search).get("internal");
18+
if (param === "1") localStorage.setItem("cpp-linter-internal", "1");
19+
if (param === "0") localStorage.removeItem("cpp-linter-internal");
20+
if (localStorage.getItem("cpp-linter-internal") === "1") {
21+
window.dataLayer = window.dataLayer || [];
22+
(function () { dataLayer.push(arguments); })("set", { traffic_type: "internal" });
23+
}
24+
} catch (e) {
25+
// localStorage is unavailable when storage is blocked; the visit is then not tagged.
26+
}
27+
</script>
28+
{% endif %}
29+
{{ super() }}
30+
{% endblock %}
31+
332
{#-
433
The site's fonts are self-hosted in docs/fonts and declared in shared.css:
534
Instrument Sans for text, JetBrains Mono for code and Bricolage Grotesque for
635
headings. theme.font is false in mkdocs.yml, so Material loads nothing itself.
736
Preloading the latin files for text and headings lets the browser fetch them
837
while it is still reading the stylesheets. The URLs must match the ones in
938
shared.css, and crossorigin is required for fonts even on the same origin.
39+
40+
The Open Graph and Twitter tags give links shared in chats and on social
41+
sites a title, a description and the card in static/og-image.png. Pages with
42+
no URL of their own, like the 404 page, leave og:url out.
1043
-#}
1144

1245
{% block extrahead %}
1346
{{ super() }}
1447
<link rel="preload" href="{{ 'fonts/instrument-sans-latin.woff2' | url }}" as="font" type="font/woff2" crossorigin>
1548
<link rel="preload" href="{{ 'fonts/bricolage-grotesque-latin.woff2' | url }}" as="font" type="font/woff2" crossorigin>
49+
{%- if page and page.meta and page.meta.title %}
50+
{%- set og_title = page.meta.title %}
51+
{%- elif page and page.title and not page.is_homepage %}
52+
{%- set og_title = page.title | striptags %}
53+
{%- else %}
54+
{%- set og_title = config.site_name %}
55+
{%- endif %}
56+
{%- set og_description = (page.meta.description if page and page.meta and page.meta.description) or config.site_description %}
57+
{%- set og_image = config.site_url ~ "static/og-image.png" %}
58+
<meta property="og:type" content="website">
59+
<meta property="og:site_name" content="{{ config.site_name }}">
60+
<meta property="og:title" content="{{ og_title }}">
61+
<meta property="og:description" content="{{ og_description }}">
62+
{%- if page and page.canonical_url %}
63+
<meta property="og:url" content="{{ page.canonical_url }}">
64+
{%- endif %}
65+
<meta property="og:image" content="{{ og_image }}">
66+
<meta property="og:image:width" content="1200">
67+
<meta property="og:image:height" content="630">
68+
<meta property="og:image:alt" content="cpp-linter: clang-format and clang-tidy for C/C++ pull requests, pre-commit hooks and CI">
69+
<meta name="twitter:card" content="summary_large_image">
70+
<meta name="twitter:title" content="{{ og_title }}">
71+
<meta name="twitter:description" content="{{ og_description }}">
72+
<meta name="twitter:image" content="{{ og_image }}">
73+
{% endblock %}
74+
75+
{#-
76+
Copying a code block is how a reader takes cpp-linter into a project, so each
77+
copy is sent to Google Analytics as a copy_code event. snippet names the
78+
section the block is in (the id of the nearest heading above it, or the
79+
block's own id on the home page), and code_title is the file name shown above
80+
the block, when it has one.
81+
-#}
82+
83+
{% block scripts %}
84+
{{ super() }}
85+
{% if config.extra.analytics %}
86+
<script>
87+
document.addEventListener("click", function (event) {
88+
var button = event.target.closest &&
89+
event.target.closest('.md-code__button[data-md-type="copy"], .md-clipboard, .cl-copy');
90+
if (!button) return;
91+
var block = button.closest(".highlight, .cl-window");
92+
var snippet = "";
93+
if (button.classList.contains("cl-copy")) {
94+
snippet = (button.getAttribute("data-clipboard-target") || "").replace(/^#/, "");
95+
} else if (block) {
96+
var headings = document.querySelectorAll("article h2[id], article h3[id]");
97+
for (var i = 0; i < headings.length; i++) {
98+
if (headings[i].compareDocumentPosition(block) & Node.DOCUMENT_POSITION_FOLLOWING) {
99+
snippet = headings[i].id;
100+
}
101+
}
102+
}
103+
var title = block && block.querySelector(".filename, .cl-window__file");
104+
window.dataLayer = window.dataLayer || [];
105+
(function () { dataLayer.push(arguments); })("event", "copy_code", {
106+
snippet: snippet || "(none)",
107+
code_title: title ? title.textContent.trim() : "(none)"
108+
});
109+
});
110+
</script>
111+
{% endif %}
16112
{% endblock %}

‎docs/robots.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The cpp-linter docs share this host: the organization site at the root and
2+
# each project's docs under its own path. This file covers all of them.
3+
User-agent: *
4+
Allow: /
5+
6+
Sitemap: https://cpp-linter.github.io/sitemap.xml
7+
Sitemap: https://cpp-linter.github.io/cpp-linter-action/sitemap.xml
8+
Sitemap: https://cpp-linter.github.io/clang-tools-pip/sitemap.xml
9+
Sitemap: https://cpp-linter.github.io/cpp-linter-rs/sitemap.xml
10+
Sitemap: https://cpp-linter.github.io/cpp-linter/sitemap.xml

‎docs/static/og-image.png‎

63 KB
Loading

‎hooks/redirects.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Write redirect pages for old URLs that still get visits.
2+
3+
GitHub Pages cannot send real redirects, so each old path gets a small page
4+
that forwards the browser with a meta refresh and tells search engines where
5+
the content lives now. The paths are the ones Google Analytics recorded as
6+
404s on this site: pages that moved into the sponsor page, short names for the
7+
getting started guide, and doc pages of cpp-linter-action, cpp-linter and
8+
cpp-linter-rs requested without their project prefix.
9+
10+
A key ending in .html is written as that file; any other key is a directory
11+
and gets an index.html. Targets are relative to site_url.
12+
"""
13+
14+
from __future__ import annotations
15+
16+
import html
17+
from pathlib import Path
18+
19+
REDIRECTS = {
20+
"docs/": "getting-started/",
21+
"merch/": "sponsor/#t-shirt-perk",
22+
"inputs-outputs/": "cpp-linter-action/inputs-outputs/",
23+
"examples/": "cpp-linter-action/examples/",
24+
"permissions/": "cpp-linter-action/permissions/",
25+
"pr-review-caveats/": "cpp-linter-action/pr-review-caveats/",
26+
"cli_args/": "cpp-linter/cli_args.html",
27+
"cpp_linter_rs/": "cpp-linter-rs/",
28+
"cpp_linter_rs/cli.html": "cpp-linter-rs/cli/",
29+
}
30+
31+
PAGE = """<!doctype html>
32+
<html lang="en">
33+
<head>
34+
<meta charset="utf-8">
35+
<title>Redirecting to {url}</title>
36+
<meta name="robots" content="noindex">
37+
<link rel="canonical" href="{url}">
38+
<meta http-equiv="refresh" content="0; url={url}">
39+
<script>location.replace({url_js})</script>
40+
</head>
41+
<body>
42+
<p>This page has moved to <a href="{url}">{url}</a>.</p>
43+
</body>
44+
</html>
45+
"""
46+
47+
48+
def on_post_build(config, **kwargs) -> None:
49+
site_dir = Path(config["site_dir"])
50+
site_url = config["site_url"]
51+
for source, target in REDIRECTS.items():
52+
path = site_dir / source
53+
if not source.endswith(".html"):
54+
path = path / "index.html"
55+
if path.exists():
56+
raise ValueError(f"redirect from {source} would replace a built page")
57+
url = site_url + target
58+
path.parent.mkdir(parents=True, exist_ok=True)
59+
path.write_text(
60+
PAGE.format(url=html.escape(url), url_js=repr(url)), encoding="utf-8"
61+
)

‎mkdocs.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ markdown_extensions:
8888
- toc:
8989
permalink: true
9090

91+
hooks:
92+
- hooks/redirects.py
93+
9194
plugins:
9295
- search
9396
- tags

0 commit comments

Comments
 (0)