Chinaunix首页 | 论坛 | 博客
  • 博客访问: 647303
  • 博文数量: 107
  • 博客积分: 4135
  • 博客等级: 上校
  • 技术积分: 1182
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-06 16:01
文章分类

全部博文(107)

文章存档

2020年(2)

2012年(5)

2011年(6)

2010年(23)

2009年(17)

2008年(35)

2007年(19)

我的朋友

分类: Web开发

2020-10-22 21:31:07

angulartics2 是angular2+ 的应用分析器支持多个分析器供应商

我选择 最流行的 Google Analytics Demo, 先看下效果

还有很多分析

Google Analytics(分析)是了解人们如何查找和使用您的网站的最受欢迎的工具。除了标准报告外,您还可以使用其用户ID功能来获取有关已注册用户的更细粒度的报告。这使您能够更好地衡量,预测并满足用户需求。

Google Analytics(分析)报告可帮助您发现用户的来源,浏览量最高的页面,访问方式,设备使用情况,显示分辨率,访问者位置,转化次数等等。用户ID跟踪功能可增强所有报告的功能。

注册Google Analytics

1. 注册

打开网址  注册登录,

2. 创建账号

 

 

3. 创建属性

 

4. 完整创建

6. 开启 user_id

7. 创建新的view, 不用默认的主要是为了显示用户名。

 

8. 查看 跟踪token

Angular 配置

1. 安装

npm install angulartics2 --save

2. 打开根目录 index.html 加入token脚本,删除脚本最后一行

  1. "utf-8">
  2. test
  3. "/">
  4. "viewport" content="width=device-width, initial-scale=1">
  5. "icon" type="image/x-icon" href="favicon.ico?v=2">

2. 到根模块(app.module.ts)里导入模块

  1. import { NgModule } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { RouterModule, Routes } from '@angular/router';
  4. import { Angulartics2Module } from 'angulartics2';
  5. import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
  6. const ROUTES: Routes = [
  7. { path: '', component: HomeComponent },
  8. { path: 'about', component: AboutComponent },
  9. ];
  10. @NgModule({
  11. imports: [
  12. BrowserModule,
  13. RouterModule.forRoot(ROUTES),
  14. // added to imports
  15. Angulartics2Module.forRoot(),
  16. ],
  17. declarations: [AppComponent],
  18. bootstrap: [AppComponent],
  19. })

2. 到 根ts文件(app.component.ts)里加入启动代码

  1. import { Angulartics2GoogleGlobalSiteTag } from 'angulartics2/gst';
  2. declare var gtag;
  3. @Component({
  4. selector: 'my-app',
  5. templateUrl: './app.component.html',
  6. styleUrls: ['./app.component.scss']
  7. })
  8. export class AppComponent {
  9. constructor(
  10. private router: Router,
  11. angulartics: Angulartics2GoogleGlobalSiteTag
  12. ) {
  13. angulartics.startTracking();
  14. const navEndEvent$ = router.events.pipe(
  15. filter(e => e instanceof NavigationEnd)
  16. );
  17. navEndEvent$.subscribe((e: NavigationEnd) => {
  18. gtag('config', 'UA-xxxxxxx-1', {'page_path':e.urlAfterRedirects}); //记得换token
  19. });
  20. }

5. 传递用户信息,在你的app 当你得到用户信息之后,例如 用户登录之后,运行脚本传递用户信息

  1. import { Angulartics2 } from 'angulartics2';
  2. declare var gtag;
  1. public getLoginUser() {
  2. let currentUser = null;
  3. const userobj = JSON.parse(localStorage.getItem(this.SESSION_KEY)).user
  4. currentUser = userobj.sub
  5. if (currentUser != null) {
  6. console.log(currentUser)
  7. gtag('set', {'user_id': currentUser}); // Set the user ID using signed-in user_id.
  8. return currentUser
  9. }
  10. }

4. 启动站点 然后访问,就这么简单。

5. 还可以自定义事件操作,我这里没做,可以自己去实践

 

Google Analytics 展示用户数据

剩下的就没什么了,就是查看了

首页:

实时数据:

位置统计

 

用户行为统计

 

根据用户名统计

用户ID 设备,以及访问记录

各种报告,还可以自定义

这些是其中一小部分,还有很多可以自己注册去看

阅读(426) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~