๐Ÿ“— Angular Google Analytics Traking

@winuss ยท January 02, 2022 ยท 3 min read

๊ตฌ๊ธ€ ๋ถ„์„์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”์ฒ™์•„์ด๋””๊ฐ€ ํฌํ•จ๋œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ index.html ํŽ˜์ด์ง€์— ์‚ฝ์ž…๋งŒ ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์—ฐ๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Angular์™€ ๊ฐ™์€ SPA๋ฐฉ์‹์€ ๋‹จ์ผํŽ˜์ด์ง€๋กœ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ๋ฐฉ์‹์œผ๋กœ ๋งŒ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ถ„์„๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ์—๋Š” ์•„์‰ฌ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ?

Angular๋Š” ๋‹จ์ผํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ธŒ๋ผ์šฐ์ €๋Š” ๊ธฐ์ˆ ์ ์œผ๋กœ ํ•œ ํŽ˜์ด์ง€๋งŒ ๋กœ๋“œํ•˜๊ณ  ํŽ˜์ด์ง€๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด DOM์€ ํ”„๋ ˆ์ž„์›Œํฌ์˜ JavaScript ์ฝ”๋“œ์— ์ง€์ •๋œ ๋Œ€๋กœ ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋งŒ ์—…๋ฐ์ดํŠธ ํ•ฉ๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ๋Š” ์‹œ๊ฐ์ ์ธ ํผ์ฆ์˜ ์ผ๋ถ€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด๊ณ  ํŠน์ • ๋ณด๊ธฐ์—์„œ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ  ์ฃผ์†Œ ํ‘œ์‹œ์ค„์˜ ๋งํฌ๊ฐ€ ์ผ์น˜ํ•˜๋„๋ก ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๋™์ผํ•œ ๋ฃจํŠธ ํŽ˜์ด์ง€์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ์•„๋™ํ•ด๋„ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ์—ฌ์ „ํžˆ ๋™์ผํ•œ ํŽ˜์ด์ง€ ์ด๊ธฐ ๋•Œ๋ฌธ์— Google Analytics ํƒœํฌ์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์€ ์ด๋ฅผ ์•Œ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ผ์šฐํ„ฐ ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ ํŽ˜์ด์ง€ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€๋กœ ์ „๋‹ฌํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

gtag.js

Google Analytics๋ฅผ ํ†ตํ•œ ์ถ”์ ์—๋Š” ga ๋ฐ gtag์˜ ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์ด ์žˆ์Šต๋‹ˆ๋‹ค. ga๋Š” ๋” ์˜ค๋ž˜๋œ ๋ฒ„์ „์ด๋ฉฐ Google์—์„œ๋Š” ๋Œ€์‹  gtag.js ๊ตฌํ˜„์„ ์ถ”์ง„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๋ ค๋ฉด Google Analytics ํŒจ๋„๋กœ ์ด๋™ํ•˜์—ฌ ๋ณต์‚ฌํ•˜์—ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋ž˜ํ‚นID๊ฐ€ ํฌํ•จ๋œ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ <head> ์„น์…˜์— ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์—์„œ ์ŠคํŠธ๋ฆผ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํŠธ๋ž˜ํ‚นID ๋ฐ HTML์— ์‚ฝ์ž…ํ•  script๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

01
01

Router Events ์ •๋ณด ์ˆ˜์ง‘

์ด์ œ ํŽ˜์ด์ง€ ์ด๋™ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Angular์—๋Š” ํŠน์ • ์ •๋ณด์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํƒ์ƒ‰ํ•œ URL์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ์— ๋ผ์šฐํ„ฐ ์ด๋ฒคํŠธ๋ฅผ ๊ตฌ๋… ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด navigationEnd ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ urlAfterRedirects๋ผ๋Š” ์†์„ฑ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์†์„ฑ์—๋Š” ๋ผ์šฐํŒ… URL์˜ / ๋ถ€๋ถ„์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์ด ๋ชจ๋“  ๊ฒƒ์„ app.component.ts ํŒŒ์ผ์— ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ ๋ชจ๋“  ๊ฒƒ์˜ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์—์„œ ๋กœ๋“œ๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ํŒŒ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ถ”์ ์„ ๋”์šฑ ๊ฐ•ํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์‹  ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก์€ Google Analytics ์ถ”์  ํŽ˜์ด์ง€ ๋ณด๊ธฐ ๋ฌธ์„œ ํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (https://developers.google.com/analytics/devguides/collection/gtagjs/pages)

...
import{Router, NavigationEnd} from '@angular/router';
...
declare let gtag: Function;
...
export class AppComponent {
  constructor(public router: Router){   
      this.router.events.subscribe(event => {
         if(event instanceof NavigationEnd){
             gtag('config', 'x-xxxxxxxxxx', 
                   {
                     'page_path': event.urlAfterRedirects
                   }
                  );
          }
       }
    )}
}

Event Tracking

์ด๋ฒคํŠธ ์ถ”์ ์€ Analytics ํ†ต๊ณ„์— ๋˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ํŠธ๋ž˜ํ‚น์€ ์›ํ•˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ์‹œ ์ˆ˜์ง‘๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Angular ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด CLI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋น„์Šค๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์ด๋ฅผ ๊ณต๊ธ‰์ž ์ค‘ ํ•˜๋‚˜๋กœ app.module.ts ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ng generate s GoogleAnalytics

app.module.ts

...
import {GoogleAnalyticsService} from './google-analytics.service';
...
@NgModule({
...
   providers: [GoogleAnalyticsService],
});
...

์ƒˆ๋กœ ์ƒ์„ฑ๋œ ์„œ๋น„์Šค ํŒŒ์ผ ๋‚ด์—์„œ gtag๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ณธ์งˆ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๊ณ  ํ˜•์‹์ด ์ •ํ™•ํ•˜๊ณ  gtag์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ธ์ง€ ํ™•์ธํ•˜๋Š” public eventEmitter ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋˜ํ•œ ์™ธ๋ถ€์—์„œ ๋กœ๋“œ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์„œ๋น„์Šค๋ฅผ ๋…ธ์ถœํ•˜๋ ค๋ฉด gtag๋ฅผ Function์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

...
declare let gtag:Function;
...
export class GoogleAnalyticsService {
...
   public eventEmitter( 
       eventName: string, 
       eventCategory: string, 
       eventAction: string, 
       eventLabel: string = null,  
       eventValue: number = null ){ 
            gtag('event', eventName, { 
                    eventCategory: eventCategory, 
                    eventLabel: eventLabel, 
                    eventAction: eventAction, 
                    eventValue: eventValue
                  })
       }
}

์ด ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ€์ ธ์™€ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ์™€ ๊ฐ™์€ ์ผ์ด ๋ฐœ์ƒํ•  ๋•Œ ํŠธ๋ž˜ํ‚น ํ•˜๋ ค๋Š” ๊ฐ’์„ eventEmitter์— ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<button (click)="clickLoginEvent()">Login</button>

๊ทธ๋Ÿฐ ๋‹ค์Œ clickLoginEvent()๋ฅผ ์ฒ˜๋ฆฌํ•  ๊ตฌ์„ฑ ์š”์†Œ์—์„œ eventEmitter๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘๋™ํ•˜๋ ค๋ฉด ์ƒ์„ฑํ•œ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

...
import{GoogleAnalyticsService} from './google-analytics.service';
...
export class LoginComponent implements OnInit {
...  
   clickLoginEvent(){ 
     this
     .googleAnalyticsService
     .eventEmitter("user_to_login", "main", "login", "click", 10);
   } 
...
}

๋‚˜๋งŒ์˜ ์ด๋ฒคํŠธ ๋ฐ ์ด๋ฒคํŠธ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Google์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฏธ๋ฆฌ ์ •์˜๋œ ๋ชฉ๋ก์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (https://developers.google.com/analytics/devguides/collection/gtagjs/events)

๋งˆ์น˜๋ฉฐ

๋‹จ์ผ ํŽ˜์ด์ง€ ๋ฐ ํ”„๋กœ๊ทธ๋ ˆ์‹œ๋ธŒ ์›น ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ์•ฝ๊ฐ„์˜ ์ฝ”๋“œ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ์—†์Šต๋‹ˆ๋‹ค. gtag.js์™€ Angular ๋ผ์šฐํŒ…์˜ ์ž‘๋™ ๋ฐฉ์‹์„ ์ดํ•ดํ•˜๊ณ  Analytics ์ถ”์ ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. angular-google-tag-manager ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ  ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. (https://www.npmjs.com/package/angular-google-tag-manager)

@winuss
Hello :) Developer notes!