Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { NavigationEnd, Router } from '@angular/router';
- import { GoogleTagManagerService } from 'angular-google-tag-manager';
- import { BlogService } from './services/blog.service';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent implements OnInit{
- title = 'ganaplayblog2';
- configCountry: any;
- constructor(
- private router: Router,
- private blogService: BlogService,
- private gtmService: GoogleTagManagerService,
- ){
- this.blogService.setCountry().subscribe(country => {
- console.log('Country set:', country);
- });
- }
- customEvent() {
- // push GTM data layer with a custom event
- const gtmTag = {
- event: 'button-click',
- data: 'my-custom-event',
- };
- this.gtmService.pushTag(gtmTag);
- alert('this is a custom event');
- }
- ngOnInit() {
- // push GTM data layer for every visited page
- this.router.events.forEach(item => {
- if (item instanceof NavigationEnd) {
- const gtmTag = {
- event: 'page',
- pageName: item.url
- };
- this.gtmService.pushTag(gtmTag);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment