Giter VIP home page Giter VIP logo

svelte-facebook-pixel's Introduction





Svelte Facebook Pixel Component

js-standard-style CircleCI Svelte v3 Svelte v3

Facebook pixel tracking component for Svelte.

Install

$ npm install --save-dev @beyonk/svelte-facebook-pixel

Usage

<FacebookPixel pixels={[ 'ABC123', '123ABC' ]} />

<script>
  import FacebookPixel from '@beyonk/svelte-facebook-pixel'
</script>

You can import FacebookPixel component whenever and wherever you want, the facebook tracking code is only initialised in the first instance.

Tracking

Simply call the track mehtod:

<FacebookPixel />

<script>
	import { FacebookPixel, fb } from '@beyonk/svelte-facebook-pixel'
	import { onMount } from 'svelte

	onMount(() => {
		fb.track('SomeEvent', { some: 'data' })
	})
</script>

Multiple Pixels on a page

You can have multiple pixels on a page, for instance, if you need a backup pixel, or if you want to send different events to different pixels.

<!-- __layout.svelte -->
<FacebookPixel />

<script>
	import { FacebookPixel, fb } from '@beyonk/svelte-facebook-pixel'
	import { onMount } from 'svelte

	onMount(() => {
		fb.track('SomeEvent', { some: 'data' })
	})
</script>

<!-- page.svelte -->

<script>
  import { fb } from '@beyonk/svelte-facebook-pixel'

	onMount(() => {
		fb.track('AnotherEvent', { some: 'data' })
	})
</script>

By default all pixels are initialised with init(), and events will be sent to all pixels, however, you can send tracking events to indiviual pixels if you would like:

Sending events to all pixels

You can send tracking events to all pixels just as you would with a single pixel:

fb.track('SomeEvent', { some: 'data' })

Sending events to a single pixel

If you have multiple pixels on your page and want to send an event to only one of them use trackSingle and pass the pixel's ID as the first argument:

fb.trackSingle('ABC123', 'SomeEvent', { some: 'data' })

Disabling the pixel (for GDPR)

If you'd like to install the pixel disabled, and enable it later after the user has consented to its use, you can do so by setting enabled: false in the pixel configuration:

<FacebookPixel enabled={false} />

Now, in your component, you can call the following in order to start the pixel and track the current page.

<FacebookPixel bind:this={_fb} {enabled} />

<script>
  import { fb } from '@beyonk/svelte-facebook-pixel'

	let _fb = null
	let enabled = false

	onMount(() => {
		enabled = true
		_fb.init()
	})
</script>

License

MIT License

Thanks

Thanks to William DASILVA for his original Nuxt facebook pixel module which inspired this project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.