Flutter web 添加google 分析 Google Analytics

最近研究flutter web 想尝试一下在web上是否有移动端的表现 ,对于一个网站,添加Google Analytics至关重要,移动端开发的小伙伴或许看到html的标签有点懵,记录一下.

前提:已经拥有google Analytics 账号,并且域名已经在平台注册

1: 在flutter 工程路径的 web层级下新建 app.js 添加如下代码⚠️注意更换成自己网站的 Google Analytics ID

//谷歌分析
function sendNavigation(location) {
    // UA-XXXXXXXXX-X 替换为 Google Analytics ID
    gtag('config', 'G-7LNM3J20ZD', { page_path: location });
}

2:在 web/index.html head标签下添加如下代码,⚠️注意还是,需要更换为自己的 Google Analytics ID

<html>
<head>
    <!--  google 分析-->
    <!-- Replace UA-XXXXXXXXX-X with Google Analytics ID -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-PC66PPZV00"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

<!--  google 分析-->
<!-- Replace UA-XXXXXXXXX-X with Google Analytics ID -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PC66PPZV00"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  // Replace UA-XXXXXXXXX-X with Google Analytics ID
  gtag('config', 'G-PC66PPZV00', { send_page_view: false });
</script>
<!-- ... -->
<script src="app.js" defer></script>
<!--  google 分析-->

3:在dart 中调用

import 'dart:js' as js;



js.context
    .callMethod('sendNavigation', [MyConStants.route]); /
import 'package:flutter/material.dart';
class MyConStants {
MyConStants.route = “/”
}

调用位置:添加部署后,多访问几次域名刷新,Google Analytics后台就有数据了

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注