8000 feat(provider): Add 'Splunk' Provider (#280) · angulartics/angulartics2@b938dfe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit b938dfe

Browse files
brunano21scttcper
authored andcommitted
feat(provider): Add 'Splunk' Provider (#280)
- **What kind of change does this PR introduce?** Add new Splunk provider - **What is the current behavior? Link to open issue?** N/A - **What is the new behavior?** N/A closes #279
1 parent 250c91b commit b938dfe

File tree

9 files changed

+221
-2
lines changed

9 files changed

+221
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ System.config({
272272
* [Woopra](/src/lib/providers/woopra)
273273
* [Clicky](/src/lib/providers/clicky)
274274
* [IBM Digital Analytics](/src/lib/providers/ibm-digital-analytics)
275+
* [Splunk](/src/lib/providers/splunk)
275276

276277
### For other providers
277278

build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const MODULE_NAMES = [
2323
'woopra',
2424
'clicky',
2525
'amplitude',
26-
'ibm-digital-analytics'
26+
'splunk',
27+
'ibm-digital-analytics',
2728
];
2829

2930

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
"Fil Maj <maj.fil@gmail.com> (https://github.com/filmaj)",
117117
"Adam Yost <swimmadude66@users.noreply.github.com> (https://github.com/swimmadude66)",
118118
"Adam S. Kirschner <accounts@adamskirschner.com> (https://github.com/hikirsch)",
119-
"Tyler Danielson <tysdanielson@gmail.com> (https://github.com/tydanielson)"
119+
"Tyler Danielson <tysdanielson@gmail.com> (https://github.com/tydanielson)",
120+
"Bruno Bruzzano <brunano21@gmail.com> (https://github.com/brunano21)"
120121
],
121122
"keywords": [
122123
"angular2",

src/app/providers/providers.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export class ProvidersComponent {
6969
name: 'segment',
7070
display: 'Segment',
7171
type: 'Analytics',
72+
}, {
73+
name: 'splunk',
74+
display: 'Splunk',
75+
type: 'Analytics',
7276
}, {
7377
name: 'woopra',
7478
display: 'Woopra',

src/assets/svg/splunk.svg

Lines changed: 41 additions & 0 deletions
Loading

src/lib/providers/splunk/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<img
2+
src="../../../assets/svg/splunk.svg"
3+
alt="Splunk logo"
4+
height="100px"
5+
width="200px" />
6+
7+
# Splunk
8+
__homepage__: [splunk.com](https://www.splunk.com/)
9+
__docs__: [Splunk Collector API](https://github.com/splunk/splunk-demo-collector-for-analyticsjs#api)
10+
__import__: `import { Angulartics2Mixpanel } from 'angulartics2/mixpanel';`
11+
12+
## Setup
13+
1. Add tracking code [provided by Splunk](https://www.splunk.com/blog/2013/10/17/still-using-3rd-party-web-analytics-providers-build-your-own-using-splunk.html) to right above the `</head>` closing tag.
14+
```
15+
<script type="text/javascript"> var sp=sp||[];(function(){var e=["init","identify","track","trackLink","pageview"],t=function(e){return function(){sp.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var n=0;n<e.length;n++)sp[e[n]]=t(e[n])})(),sp.load=function(e,o){sp._endpoint=e;if(o){sp.init(o)};var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d21ey8j28ejz92.cloudfront.net/analytics/v1/sp.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)};
16+
sp.load("https://www.example.com"); // Replace with your own collector URL
17+
</script>
18+
```
19+
In the last line of above script, make sure to replace https://www.example.com with the address of your data collector.
20+
21+
2. [Setup Angulartics](https://github.com/angulartics/angulartics2/tree/next#installation) using `Angulartics2Splunk`.
22+
```ts
23+
import { NgModule } from '@angular/core';
24+
import { BrowserModule } from '@angular/platform-browser';
25+
import { RouterModule, Routes } from '@angular/router';
26+
27+
import { Angulartics2Module } from 'angulartics2';
28+
import { Angulartics2Splunk } from 'angulartics2/splunk';
29+
30+
const ROUTES: Routes = [
31+
{ path: '', component: HomeComponent },
32+
{ path: 'about', component: AboutComponent },
33+
];
34+
35+
@NgModule({
36+
imports: [
37+
BrowserModule,
38+
RouterModule.forRoot(ROUTES),
39+
40+
// added to imports
41+
Angulartics2Module.forRoot([Angulartics2Splunk]),
42+
],
43+
declarations: [AppComponent],
44+
bootstrap: [AppComponent],
45+
})
46+
```
47+
3. __Required__: Import your providers in the root component. This starts the tracking of route changes.
48+
```ts
49+
// component
50+
import { Angulartics2Splunk } from 'angulartics2/splunk';
51+
52+
@Component({ ... })
53+
export class AppComponent {
54+
constructor(angulartics2Splunk: Angulartics2Splunk) {}
55+
}
56+
```

src/lib/providers/splunk/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "../../../../node_modules/ng-packagr/package.schema.json",
3+
"name": "angulartics2/splunk",
4+
"description": "The splunk module",
5+
"homepage": "https://angulartics.github.io/angulartics2/",
6+
"author": "Bruno Bruzzano <brunano21@gmail.com> (http://github.com/brunano21)",
7+
"repository": "angulartics/angulartics2",
8+
"license": "MIT",
9+
"ngPackage": {
10+
"lib": {
11+
"entryFile": "splunk.ts",
12+
"umdModuleIds": {
13+
"angulartics2": "angulartics2"
14+
}
15+
},
16+
"dest": "../../../../dist/packages-dist/splunk"
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
fakeAsync,
3+
inject,
4+
ComponentFixture,
5+
TestBed,
6+
} from '@angular/core/testing';
7+
8+
import { Angulartics2 } from 'angulartics2';
9+
import { advance, createRoot, RootCmp, TestModule } from '../../test.mocks';
10+
import { Angulartics2Splunk } from './splunk';
11+
12+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
13+
declare var window: any;
14+
15+
describe('Angulartics2Splunk', () => {
16+
17+
let fixture: ComponentFixture<any>;
18+
let sp: any;
19+
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
22+
imports: [TestModule],
23+
providers: [Angulartics2Splunk],
24+
});
25+
26+
window.sp = sp = {
27+
pageview: jasmine.createSpy('pageview'),
28+
track: jasmine.createSpy('track')
29+
};
30+
});
31+
32+
it('should track pages',
33+
fakeAsync(inject([Angulartics2, Angulartics2Splunk],
34+
(angulartics2: Angulartics2, angulartics2Splunk: Angulartics2Splunk) => {
35+
fixture = createRoot(RootCmp);
36+
angulartics2.pageTrack.next({ path: '/abc' });
37+
advance(fixture);
38+
expect(sp.pageview).toHaveBeenCalledWith('/abc');
39+
}),
40+
),
41+
);
42+
43+
it('should track events',
44+
fakeAsync(inject([Angulartics2, Angulartics2Splunk],
45+
(angulartics2: Angulartics2, angulartics2Splunk: Angulartics2Splunk) => {
46+
fixture = createRoot(RootCmp);
47+
angulartics2.eventTrack.next({ action: 'do', properties: { category: 'cat' } });
48+
advance(fixture);
49+
expect(sp.track).toHaveBeenCalledWith('do', {
50+
category: 'cat',
51+
});
52+
}),
53+
),
54+
);
55+
});

src/lib/providers/splunk/splunk.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { Angulartics2 } from 'angulartics2';
4+
5+
declare var sp: any;
6+
7+
@Injectable()
8+
export class Angulartics2Splunk {
9+
10+
constructor(private angulartics2: Angulartics2) {
11+
if (typeof (sp) === 'undefined') {
12+
console.warn('Splunk not found');
13+
}
14+
15+
this.angulartics2.pageTrack
16+
.pipe(this.angulartics2.filterDeveloperMode())
17+
.subscribe((x) => this.pageTrack(x.path));
18+
this.angulartics2.eventTrack
19+
.pipe(this.angulartics2.filterDeveloperMode())
20+
.subscribe((x) => this.eventTrack(x.action, x.properties));
21+
}
22+
23+
pageTrack(path: string) {
24+
try {
25+
sp.pageview(path);
26+
} catch (e) {
27+
if (!(e instanceof ReferenceError)) {
28+
throw e;
29+
}
30+
}
31+
}
32+
33+
eventTrack(action: string, properties: any) {
34+
try {
35+
sp.track(action, properties);
36+
} catch (e) {
37+
if (!(e instanceof ReferenceError)) {
38+
throw e;
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)
0