惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
罗磊的独立博客
小众软件
小众软件
I
InfoQ
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno
How to use Google Analytics in Deno Deploy | Deno
2022-03-08 · via Deno

Please note that this blog post is about Google Universal Analytics, which was replaced by GA4 in 2024.


Google Analytics is a popular and simple way to get basic analytics on your site. While dropping GA’s JavaScript snippet on the client-side is easy, it doesn’t work when visitors use ad blockers. (For example, over 70% of our audience uses ad blockers.) Using server-side GA not only avoids ad blockers, but also is more reliable and accurate.

Deno Deploy

Deno Deploy is our edge computing service that runs modern JavaScript and TypeScript around the world. Services on Deno Deploy are highly available with minimal latency, which can be used for a variety of use cases, from static sites and e-commerce stores to oauth micro services. (This blog that you are reading right now is hosted on Deno Deploy!)

No matter what your use case is with Deno Deploy, now you can easily add Google Analytics to better understand how users are engaging with your website or service.

Example

We wrote a GA integration as a module, which can be imported directly into your Deno CLI or Deploy Project. Note that the module currently supports the classic Universal Analytics, though we are investigating moving to GA4.

import { createReporter } from "https://deno.land/x/g_a/mod.ts";
import { serve } from "https://deno.land/std/http/server.ts";

const ga = createReporter({ id: "UA-123" });

serve((req, connInfo) => {
  let err;
  let res;
  const start = performance.now();
  try {
    res = new Response("hello world");
  } catch (e) {
    err = e;
  } finally {
    ga(req, connInfo, res, start, err);
  }
  return res;
});

Or try this example in a Deno Deploy playground: server-side-ga.

If you’re using Deno’s HTTP middleware server oak, then you can also add GA as middleware:

import { createReportMiddleware } from "https://deno.land/x/g_a/mod.ts";
import { Application } from "https://deno.land/x/oak/mod.ts";

const ga = createReportMiddleware({ id: "UA-123" });
const app = new Application();

app.use(ga);
app.use((ctx) => {
  ctx.response.body = "Hello World!";
});

app.listen();

You can also set your UA property as an environmental variable, GA_TRACKING_ID in the Deploy settings:

Setting GA_TRACKING_ID as an environmental variable

And the above integration will work right out of the box:

Google Analytics Real-time Reporting

What’s next

Google Analytics is a powerful and free analytics tool. We’re currently investigating moving to GA4, as well as adding support for sending events. If you’re using this GA module, we’d love to get your feedback.