1
1
import { Component } from "@angular/core" ;
2
- import { Title } from "@angular/platform-browser" ;
2
+ import { Title , Meta } from "@angular/platform-browser" ;
3
3
import { Router , NavigationEnd } from "@angular/router" ;
4
4
5
5
import { GoogleAnalyticsService } from "./shared/" ;
@@ -11,23 +11,53 @@ import { GoogleAnalyticsService } from "./shared/";
11
11
} )
12
12
export class AppComponent {
13
13
routes = {
14
- "/" : "Home Page" ,
15
- "/home" : "Home Page" ,
16
- "/about" : "About Us" ,
17
- "/contact" : "Contact Us" ,
14
+ "/" : {
15
+ title : "Home Page" ,
16
+ desc : "This is the description of Home Page"
17
+ } ,
18
+ "/home" : {
19
+ title : "Home Page" ,
20
+ desc : "This is the description of Home Page"
21
+ } ,
22
+ "/about" : {
23
+ title : "About Us" ,
24
+ desc : "This page will describe some things about us"
25
+ } ,
26
+ "/contact" : {
27
+ title : "Contact Us" ,
28
+ desc : "This page will tell you how to contact us"
29
+ }
18
30
} ;
19
31
20
32
constructor (
21
33
public router : Router ,
22
34
private googleAnalyticsService : GoogleAnalyticsService ,
23
- private titleService : Title
35
+ private titleService : Title ,
36
+ private meta : Meta
24
37
) {
25
38
this . router . events . subscribe ( event => {
26
39
if ( event instanceof NavigationEnd ) {
27
- this . titleService . setTitle ( this . routes [ event . url ] ) ;
40
+ this . handleSEO ( event ) ;
28
41
29
42
this . googleAnalyticsService . emitPageEvent ( event ) ;
30
43
}
31
44
} ) ;
32
45
}
46
+
47
+ handleSEO ( event : NavigationEnd ) {
48
+ const desc = this . routes [ event . url ] . desc ;
49
+
50
+ // Generic
51
+ this . titleService . setTitle ( this . routes [ event . url ] . title ) ;
52
+ this . meta . addTag ( { name : "description" , content : desc } ) ;
53
+
54
+ // Twitter Metadata
55
+ this . meta . addTag ( { name : "twitter:card" , content : "summary_large_image" } ) ;
56
+ this . meta . addTag ( { name : "twitter:site" , content : "@AbhijitKarDikha" } ) ;
57
+ this . meta . addTag ( { name : "twitter:creator" , content : "@AbhijitKarDikha" } ) ;
58
+ this . meta . addTag ( { name : "twitter:title" , content : desc } ) ;
59
+ this . meta . addTag ( { name : "twitter:description" , content : desc } ) ;
60
+ this . meta . addTag ( { name : "twitter:text:description" , content : desc } ) ;
61
+ this . meta . addTag ( { name : "twitter:image" , content : "https://www.abhijit-kar.com/assets/meta/screenshot.png" } ) ;
62
+ }
33
63
}
0 commit comments