Skip to content

Commit

Permalink
feat: add disable method (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDASILVA committed Nov 1, 2020
1 parent 83f06b2 commit 50ce676
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 1,064 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Now, in your component, you can call the following in order to start the pixel a
this.$fb.enable()
```

The pixel can be disabled again later on by using the `.disable()` method.

## Module options

List of possible options in the module:
Expand All @@ -96,7 +98,7 @@ List of possible options in the module:
| pixelId | null | true | The unique pixel identifier provided by Facebook. |
| track | PageView | false | Default tracking event. |
| version | 2.0 | false | Tracking version. |
| disabled | false | false | Disable the Pixel by default when initialized. Can be enabled later through `$fb.enable()`.
| disabled | false | false | Disable the Pixel by default when initialized. Can be enabled later through `$fb.enable()` and disabled again with `$fb.disable()`.

## Facebook pixel instance

Expand All @@ -105,6 +107,7 @@ The tracking pixel instance is available on all vue component instances as $fb.
| Method | Purpose | Equivalent to |
|-------------------|----------------------------------------------------------------------------------------------------------|--------------------------------|
| enable() | If you had previously set `disabled: true` in config, enables the pixel and tracks the current page view | $fb.init(), $fb.track() |
| disable() | Disables the pixel again | |
| init() | Initialises the pixel | fbq('init', <options.pixelId>) |
| track(event, parameters) | Sends a track event with optional `parameters`. It's `PageView` by default if the `event` is not defined. | fbq('track', <options.track>, parameters) |
| query(key, value, parameters) | Call the underlying fbq instance with anything else. The `parameters` attribute is optional. | fbq(key, value, parameters) |
Expand Down
14 changes: 13 additions & 1 deletion lib/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
*/
class Fb {
constructor (fbq, options) {
this.fbq = fbq
this.options = options
this.fbq = fbq

this.isEnabled = !options.disabled
}

/**
* @method enable
*/
enable () {
this.isEnabled = true
this.init()
this.track()
}

/**
* @method disable
*/
disable () {
this.isEnabled = false
}

/**
* @method init
*/
Expand All @@ -40,6 +50,8 @@ class Fb {
* @param {object} parameters
*/
query (cmd, option, parameters = null) {
if (!this.isEnabled) return

if (!parameters) {
this.fbq(cmd, option)
} else {
Expand Down

0 comments on commit 50ce676

Please sign in to comment.