Skip to content

Commit fdad2d1

Browse files
committed
Merge branch 'develop' into main
2 parents 4155841 + db7f278 commit fdad2d1

19 files changed

+13153
-3265
lines changed

README.md

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,104 @@
1-
# NgxPixelWorkspace
1+
# ngx-pixel
22

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.0.2.
3+
![](https://storage.googleapis.com/nielskersic/static-images/github/Angular%20%2B%20FB%20Event%20Mgr.png)
44

5-
## Development server
5+
<center>
6+
A simple Angular library to simplify tracking using a Facebook Pixel.
7+
</center>
68

7-
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
9+
---
810

9-
## Code scaffolding
11+
## Introduction
12+
Using a Facebook Pixel is fairly simple. You add the script to the `head` section of all pages, after which you can use the `fbq` function. However, in Angular it's not as straight-forward. The main two problems are as follows:
1013

11-
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
14+
- Navigation using the Angular Router is not tracked.
15+
- There are no TypeScript definitions by default, so no Intellisense or linting.
1216

13-
## Build
17+
***ngx-pixel*** solves both of these issues.
1418

15-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
19+
## Usage
20+
Using ***ngx-pixel*** is very simple.
21+
22+
### 1. Installing the NPM package
23+
You can install the NPM package with `npm install ngx-pixel`
24+
25+
### 2. Add it to your app module
26+
Add the library to your app's module, usually `app.module.ts`. Make sure you use the `forRoot()` method. Within this method, add your [Facebook Pixel ID](https://www.facebook.com/business/help/952192354843755).
27+
```typescript
28+
import { PixelModule } from 'ngx-pixel';
29+
30+
@NgModule({
31+
declarations: [
32+
AppComponent
33+
],
34+
imports: [
35+
BrowserModule,
36+
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'})
37+
],
38+
providers: [],
39+
bootstrap: [AppComponent]
40+
})
41+
export class AppModule { }
42+
```
43+
**NOTE:** By default, the library does not start tracking immediately. In most cases this requires user consent to comply with GDPR and other privacy regulations. If you would still like start tracking as soon as your app module loads, include the `enabled: true` parameter in when you call `forRoot()`.
44+
45+
### 3. Add it to your components
46+
In any component where you would like to track certain events, you can import the ***ngx-pixel service*** with `import { PixelService } from 'ngx-pixel';`
47+
Then you can inject the service into your component as follows:
48+
```TypeScript
49+
export class HomeComponent {
50+
51+
constructor(
52+
private pixel: PixelService
53+
) { }
54+
55+
}
56+
```
57+
58+
### 4. Tracking events
59+
There are two groups of events that you can track, namely *Standard events* and *Custom events*.
60+
61+
#### Standard events
62+
**Standard events** are common events that Facebook has created. You can find the full list [here](https://developers.facebook.com/docs/facebook-pixel/reference). You can track a Standard event like this:
63+
![Track Standard events using ngx-pixel](https://storage.googleapis.com/nielskersic/static-images/github/ngx-pixel-track-large.gif)
64+
65+
The first argument is the type of event as defined by Facebook. The optional second argument can be used to pass more information about the event. E.g.:
66+
```typescript
67+
this.pixel.track('InitiateCheckout', {
68+
content_ids: ['ABC123', 'XYZ456'], // Item SKUs
69+
value: 100, // Value of all items
70+
currency: 'USD' // Currency of the value
71+
});
72+
```
73+
74+
#### Custom events
75+
Tracking **Custom events** works very similar to tracking Standard events. The only major difference is that there are no TypeScript interfaces and therefore no Intellisense. This is because the event *name* and optional *properties* can be anything. Tracking a custom event with ***ngx-pixel*** looks like this:
76+
```TypeScript
77+
this.pixel.trackCustom('MyCustomEvent');
78+
```
79+
80+
And just like with Standard events, you can add more properties. This is recommended, since this enables you to create even more specific audiences within Facebook Business Manager. Which properties you add is completely up to you. Here is an example:
81+
```TypeScript
82+
this.pixel.trackCustom('MyCustomEvent', {
83+
platform: 'limewire'
84+
})
85+
```
86+
87+
## Important notes
88+
- ***ngx-pixel*** was developed using Angular 11, which uses the Ivy compiler instead of the older View Engine compiler. If your project uses Angular 8 or earlier, or if you decided to keep using View Engine with newer Angular versions, ***ngx-pixel*** might not be compatible, although I have not yet tested this to confirm.
89+
90+
## What's next?
91+
- [ ] Checking Pixel ID's using a RegEx. I first want to confirm whether all Pixel ID's follow the same format.
92+
- [ ] Adding tests.
93+
- [ ] Testing View Engine backwards-compatibility.
94+
95+
---
96+
<center>
97+
Created with ❤️ by Niels Kersic, [niels.codes](https://niels.codes).
98+
</center>
1699

17-
## Running unit tests
18100

19-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
20101

21-
## Running end-to-end tests
22102

23-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
24103

25-
## Further help
26104

27-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.

angular.json

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,48 @@
33
"version": 1,
44
"newProjectRoot": "projects",
55
"projects": {
6-
}
7-
}
6+
"pixel": {
7+
"projectType": "library",
8+
"root": "projects/pixel",
9+
"sourceRoot": "projects/pixel/src",
10+
"prefix": "lib",
11+
"architect": {
12+
"build": {
13+
"builder": "@angular-devkit/build-angular:ng-packagr",
14+
"options": {
15+
"tsConfig": "projects/pixel/tsconfig.lib.json",
16+
"project": "projects/pixel/ng-package.json"
17+
},
18+
"configurations": {
19+
"production": {
20+
"tsConfig": "projects/pixel/tsconfig.lib.prod.json"
21+
}
22+
}
23+
},
24+
"test": {
25+
"builder": "@angular-devkit/build-angular:karma",
26+
"options": {
27+
"main": "projects/pixel/src/test.ts",
28+
"tsConfig": "projects/pixel/tsconfig.spec.json",
29+
"karmaConfig": "projects/pixel/karma.conf.js"
30+
}
31+
},
32+
"lint": {
33+
"builder": "@angular-devkit/build-angular:tslint",
34+
"options": {
35+
"tsConfig": [
36+
"projects/pixel/tsconfig.lib.json",
37+
"projects/pixel/tsconfig.spec.json"
38+
],
39+
"exclude": [
40+
"**/node_modules/**"
41+
]
42+
}
43+
}
44+
}
45+
}},
46+
"cli": {
47+
"analytics": "236561c0-2dd3-46bd-9c4c-605442e3815b"
48+
},
49+
"defaultProject": "pixel"
50+
}

0 commit comments

Comments
 (0)