• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    TypeScript
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”– Google Analytics gtag.js for Angular

Angular gtag.js

A simple Google Analytics gtag.js package for Angular.

Install

npm install angular-gtag --save

Add the the tracking code from GA admin dashboard to index.html and set send_page_view to false.

<head>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-YOUR_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-YOUR_TRACKING_ID', { 'send_page_view': false });
</script>

</head>

Add the package to to your app.module.ts.

import { GtagModule } from 'angular-gtag';

@NgModule({
  imports: [
    GtagModule.forRoot({ trackingId: 'UA-YOUR_TRACKING_ID', trackPageviews: true })
  ]
})

Options

  • trackingId: string (required) Google Analytics UA tracking ID.
  • trackPageviews: boolean Default true.
  • debug: boolean Default false, console logs every gtag event/pageview.

Pageviews

The package will listen to route changes by default, you just need to instantiate service in the root of the project.

export class AppComponent {
  constructor(gtag: Gtag) {}
}

Gtag is a serivce that also allows you to track pageviews manually.

gtag.pageview();

// or with custom params

gtag.pageview({
  page_title: 'Lesson Feed',
  page_path: '/lessons',
  page_location: 'https://angularfirebase.com/lessons'
});

Events

Events expect an action.

gtag.event('view_promotion');

You can optionally pass in addtional params.

gtag.event('login', {
  method: 'Instagram',
  event_category: 'engagemnt',
  event_label: 'New user logged in via OAuth'
});

Event Directive

Many analytics events are tracked based on user interaction, such as button clicks. Just tell it which DOM event to track.

<button gtagEvent trackOn="click">Track Me</button>

This will register a general event in GA based on the event name.

You can pass optional params to the directive like so:

<div gtagEvent
     trackOn="dragstart"
     action="product_dragged"
     category="ecommerce"
     [params]="{ event_label: 'Something cool just happened' }">

   Some Product...

</div>

The directive will produce the following event on dragstart.

More Repositories

1

sveltefire

Cybernetically enhanced Firebase apps
Svelte
955
star
2

angular-firestarter

🍱 πŸ”₯ Angular + Firebase Progressive Web App Starter
TypeScript
931
star
3

rektor-db

Rektor Vector Database
588
star
4

code-this-not-that-js

JavaScript Pro Tips - Code This, Not That
JavaScript
512
star
5

geofirex

🌐 πŸ“ Geolocation Queries with Firestore & RxJS
TypeScript
460
star
6

firestore-migrator

πŸš„ A CLI utility for moving data to and from Cloud Firestore
TypeScript
311
star
7

async-await-pro-tips

TypeScript
232
star
8

electron-forge-svelte

A starter template for Electron Forge + Svelte 3
JavaScript
132
star
9

gimmie-sticker

Trade a Pull Request for a Sticker
JavaScript
123
star
10

ngrx-vs-ngxs

A side-by-side comparison of Angular state management libraries
TypeScript
116
star
11

cloud-functions-master-course

Firebase Cloud Functions Master Course
HTML
113
star
12

ngrx-fire

Demo app using Angular + ngrx + Firebase
TypeScript
109
star
13

angular-firebase-stripe

Full Stack Stripe Payments Solution with Angular + Firebase
TypeScript
88
star
14

ionic4-master-course

Source Code for the Ionic 4 Master Course
TypeScript
67
star
15

sveltefire-template

App template for SvelteFire
HTML
52
star
16

ionic-firestarter

Ionic Firebase Starter Template
TypeScript
44
star
17

stripe-firebase-master-course

44
star
18

hnpwa-angular5

Hacker News PWA with an Angular 5 Service Worker
HTML
42
star
19

vue-firestore-hello-world

Quickstart using Webpack, Vuefire, and Firestore
JavaScript
23
star
20

magic-music-flutter-create

Dart
19
star
21

can-i-use-ng-add

Where can I use the `ng add` command?
16
star
22

hippicons

an alternative icon font for the web
HTML
13
star
23

flowmap

Stream and slice data
Dart
13
star
24

javascript-jeopardy

A Realtime Trivia Game
HTML
12
star
25

lazy-loading-angular

A bare minimum demo for Component Lazy Loading in Angular 4
TypeScript
11
star
26

lex-chatbot-lambda

Python validation for Lex. AWS Chatbot Hackathon 2017
Python
10
star
27

self-driving-car-projects

Self Driving Car Projects
Jupyter Notebook
10
star
28

machine-learning-nanodegree

Machine learning projects and examples
HTML
10
star
29

tasksnail

Rails, AJAX, and Devise - Single Page Application Demo
CSS
9
star
30

hotroute

TypeScript
8
star
31

anchorage

Community Powered Cruising Guide for Boaters
JavaScript
7
star
32

lightstate

Experimental StatefulObject
TypeScript
5
star
33

firestarter-geofire

TypeScript
5
star
34

nice-tweets-only

Sentiment Validation with Ionic v4 + Cloud Language API + Cloud Functions
TypeScript
5
star
35

icon-font-lesson

codediode.io lesson for creating custom icon fonts
CSS
4
star
36

brachial-plexus-ultrasound-segmentation

Neural network comparison for Kaggle Ultrasound Segmentation competition
Jupyter Notebook
3
star
37

rembg-webapp-tutorial

a simple webapp with rembg
HTML
3
star
38

kaggle-scripts

Data visualization and machine learning scripts on Kaggle
Jupyter Notebook
3
star
39

juicesherpa

Build Juice Recipes with Realtime Nutrition Facts
Ruby
2
star
40

platform

A digital sandbox of Matthew harwood
TypeScript
2
star