Skip to content

Commit

Permalink
feat() add BalGoogleTagManager and BalGoogleMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
nobilo committed Jun 8, 2021
1 parent c1458c5 commit e7328eb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
19 changes: 19 additions & 0 deletions rollup.config.js
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: [],
};
43 changes: 25 additions & 18 deletions src/google-maps.ts
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);
}
}
35 changes: 35 additions & 0 deletions src/google-tag-manager.ts
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 */
}
}
3 changes: 2 additions & 1 deletion src/index.ts
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';

0 comments on commit e7328eb

Please sign in to comment.