Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 8bd1c57

Browse files
author
Elliott Grieco
authored
Merge pull request #6 from NYULibraries/feature/target_document
Feature/target document
2 parents 74a5f9c + 52ef70c commit 8bd1c57

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ This will add the necessary script tags to the bottom of the `head` of your web
6060

6161
You'll need to configure the module by passing it an object as an angular `constant` named `googleAnalyticsConfig`.
6262

63+
Optionally, you can also use an array of configurations if you need to add multiple tag managers to your page.
64+
6365
#### Required
6466
| name | type | usage |
6567
|------|-------------|--------|
@@ -85,7 +87,8 @@ You'll need to configure the module by passing it an object as an angular `const
8587
| name | type | usage |
8688
|------|-------------|--------|
8789
| `externalScriptURL` | `string` | If you are using an alternative URL, specify the source of the external script that is loaded. Use `null` if you don't want an external script to be loaded (especially for legacy Google Analytics using an `inlineScript`) |
88-
| `inlineScript` | `string` | Specify the inline script tag to be inserted below the external script tag. ||
90+
| `inlineScript` | `string` | Specify the inline script tag to be inserted below the external script tag. |
91+
| `target` | `string` | (default: `'head'`) What element on the document you would like to append the injected script to. E.g. `body`. Uses `document.querySelector` to find the first instance of the element. ||
8992

9093
#### Customization Example
9194

@@ -105,7 +108,8 @@ app.constant('googleAnalyticsConfig', {
105108
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
106109
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
107110
})();
108-
`
111+
`,
112+
target: 'body',
109113
})
110114
```
111115
output:

src/js/googleAnalytics.module.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "angulartics";
22
import "angulartics-google-tag-manager";
33

4-
function buildConfig({ externalScriptURL, inlineScript, trackingId }) {
4+
function buildConfig({ externalScriptURL, inlineScript, trackingId, target }) {
55
const defaultCode = `window.dataLayer = window.dataLayer || [];
66
function gtag(){dataLayer.push(arguments);}
77
gtag('js', new Date());
@@ -11,6 +11,7 @@ function buildConfig({ externalScriptURL, inlineScript, trackingId }) {
1111
return {
1212
externalSource: externalScriptURL === undefined ? defaultURL : externalScriptURL,
1313
inlineCode: inlineScript || defaultCode,
14+
target: target || 'head',
1415
};
1516
}
1617

@@ -24,11 +25,11 @@ angular
2425

2526
return ({
2627
injectGACode: () => {
27-
configs.forEach(({ externalSource, inlineCode }) => {
28+
configs.forEach(({ externalSource, inlineCode, target }) => {
2829
if (externalSource !== null) {
2930
const externalScriptTag = document.createElement('script');
3031
externalScriptTag.src = externalSource;
31-
document.head.appendChild(externalScriptTag);
32+
document.querySelector(target).appendChild(externalScriptTag);
3233
}
3334

3435
const inlineScriptTag = document.createElement('script');
@@ -41,7 +42,7 @@ angular
4142
inlineScriptTag.text = inlineCode;
4243
}
4344

44-
document.head.appendChild(inlineScriptTag);
45+
document.querySelector(target).appendChild(inlineScriptTag);
4546
});
4647
},
4748
});

src/spec/googleAnalytics.factory.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,35 @@ describe('googleAnalyticsService', () => {
142142
expect(innerText).toEqual(googleAnalyticsConfigWithNullExternalURL.inlineScript);
143143
});
144144
});
145+
146+
describe('with a defined target', () => {
147+
beforeEach(module('googleAnalytics', ($provide) => {
148+
$provide.constant('googleAnalyticsConfig', Object.assign({}, googleAnalyticsConfigWithDefaults, { target: 'body' }));
149+
}));
150+
151+
let gaInjectionService;
152+
beforeEach(inject((_gaInjectionService_) => {
153+
gaInjectionService = _gaInjectionService_;
154+
}));
155+
156+
describe('injectGACode', () => {
157+
let scripts;
158+
beforeEach(() => {
159+
const getScripts = () => document.querySelector('body').querySelectorAll('script');
160+
Array.from(getScripts()).forEach(script => script.parentNode.removeChild(script));
161+
gaInjectionService.injectGACode();
162+
scripts = getScripts();
163+
});
164+
165+
it('should add the default external script tag to the specified target', () => {
166+
const src = scripts[0].src;
167+
expect(src).toEqual(`https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsConfig.trackingId}`);
168+
});
169+
170+
it('should add the default inline script tag to the specified target', () => {
171+
const innerText = scripts[1].innerText.replace(/\s+/g, ' ').trim();
172+
expect(innerText).toContain(`gtag('config', '${googleAnalyticsConfig.trackingId}')`);
173+
});
174+
});
175+
});
145176
});

0 commit comments

Comments
 (0)