-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat() add BalGoogleTagManager and BalGoogleMaps
- Loading branch information
Showing
4 changed files
with
81 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default { | ||
input: "dist-transpiled/index.js", | ||
output: [ | ||
{ | ||
dir: "dist/", | ||
entryFileNames: "[name].esm.js", | ||
chunkFileNames: "[name]-[hash].esm.js", | ||
format: "es", | ||
sourcemap: true, | ||
}, | ||
{ | ||
dir: "dist/", | ||
format: "commonjs", | ||
preferConst: true, | ||
sourcemap: true, | ||
}, | ||
], | ||
external: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
|
||
const GOOGLE = 'google'; | ||
const getGoogleMapsSrc = (version: string, apiKey: string) => | ||
`//maps.googleapis.com/maps/api/js?libraries=places&v=${version}&key=${apiKey}`; | ||
`//maps.googleapis.com/maps/api/js?libraries=places&v=${version}&key=${apiKey}`; | ||
|
||
export default class BalGoogleMaps { | ||
public static isEnabled(): boolean { | ||
return (window as any)[GOOGLE] != null | ||
&& (window as any)[GOOGLE].maps != null; | ||
} | ||
|
||
|
||
public static load(settings: { version: string, apiKey: string }): void { | ||
if (!this.isEnabled()) { | ||
this.loadScript(getGoogleMapsSrc(settings.version, settings.apiKey)); | ||
} else { | ||
throw new Error('Google Maps API loaded already'); | ||
} | ||
} | ||
|
||
|
||
// | ||
// export class BalGoogleMapsService implements ThirdPartyApiService { | ||
// constructor(private windowRefService: WindowRefService) { | ||
// } | ||
// isEnabled(): boolean { | ||
// return this.windowRefService.nativeWindow[GOOGLE] != null | ||
// && this.windowRefService.nativeWindow[GOOGLE].maps != null; | ||
// } | ||
// load(settings: { version: string, apiKey: string }): void { | ||
// if (!this.isEnabled()) { | ||
// this.windowRefService.loadScript(getGoogleMapsSrc(settings.version, settings.apiKey)); | ||
// } else { | ||
// throw new Error('Google Maps API loaded already'); | ||
// } | ||
// } | ||
// } | ||
private static loadScript(src: string): void { | ||
let node = document.createElement('script'); | ||
node.src = src; | ||
node.type = 'text/javascript'; | ||
node.async = false; | ||
document.getElementsByTagName('head')[0].appendChild(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const DATA_LAYER = 'dataLayer'; | ||
|
||
export default class BalGoogleTagManager { | ||
public static isEnabled(): boolean { | ||
return (window as any)[DATA_LAYER] != null; | ||
} | ||
|
||
public static load(settings: { apiKey: string }): void { | ||
if (!this.isEnabled()) { | ||
this.runGtmScript(settings.apiKey); | ||
} else { | ||
throw new Error('Google Tag Manager API loaded already'); | ||
} | ||
} | ||
|
||
public static runGtmScript(apiKey: string): void { | ||
/*tslint:disable */ | ||
let w: any = window; | ||
let d = document; | ||
let s = 'script'; | ||
let l = DATA_LAYER; | ||
w[l] = w[l] || []; | ||
w[l].push({ | ||
'gtm.start': | ||
new Date().getTime(), event: 'gtm.js' | ||
}); | ||
let f = d.getElementsByTagName(s)[0], | ||
j: any = d.createElement(s), dl = l != DATA_LAYER ? '&l=' + l : ''; | ||
j.async = true; | ||
j.src = | ||
'https://www.googletagmanager.com/gtm.js?id=' + apiKey + dl; | ||
f.parentNode.insertBefore(j, f); | ||
/*tslint:enable */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./google-maps"; | ||
export * from './google-maps'; | ||
export * from './google-tag-manager'; |