alferesw

app.component.ts

Dec 10th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { NavigationEnd, Router } from '@angular/router';
  3. import { GoogleTagManagerService } from 'angular-google-tag-manager';
  4. import { BlogService } from './services/blog.service';
  5.  
  6.  
  7. @Component({
  8. selector: 'app-root',
  9. templateUrl: './app.component.html',
  10. styleUrls: ['./app.component.css']
  11. })
  12. export class AppComponent implements OnInit{
  13. title = 'ganaplayblog2';
  14. configCountry: any;
  15.  
  16. constructor(
  17. private router: Router,
  18. private blogService: BlogService,
  19. private gtmService: GoogleTagManagerService,
  20. ){
  21. this.blogService.setCountry().subscribe(country => {
  22. console.log('Country set:', country);
  23. });
  24. }
  25.  
  26. customEvent() {
  27.  
  28. // push GTM data layer with a custom event
  29. const gtmTag = {
  30. event: 'button-click',
  31. data: 'my-custom-event',
  32. };
  33. this.gtmService.pushTag(gtmTag);
  34.  
  35. alert('this is a custom event');
  36. }
  37.  
  38. ngOnInit() {
  39. // push GTM data layer for every visited page
  40. this.router.events.forEach(item => {
  41. if (item instanceof NavigationEnd) {
  42. const gtmTag = {
  43. event: 'page',
  44. pageName: item.url
  45. };
  46.  
  47. this.gtmService.pushTag(gtmTag);
  48. }
  49. });
  50. }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment