beyonk-group / svelte-facebook-pixel Goto Github PK
View Code? Open in Web Editor NEWA facebook pixel module for Svelte / SvelteKit
A facebook pixel module for Svelte / SvelteKit
The init function as it's currently implemented does not work with SvelteKit. If you have preloading enabled in SvelteKit, then javascript is loaded in link tags instead of script tags, so the follow function fails.
export function init () {
const fn = function (f, b, e, v, n, t, s) {
if (f.fbq) return
n = f.fbq = function () {
n.callMethod
? n.callMethod.apply(n, arguments) : n.queue.push(arguments)
}
if (!f._fbq) f._fbq = n
n.push = n
n.loaded = !0
n.version = version
n.queue = []
t = b.createElement(e)
t.async = !0
t.src = v
s = b.getElementsByTagName(e)[0]
s.parentNode.insertBefore(t, s)
}
fn(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js')
callback()
}
I made it work for my purposes by changing the line to s = b.getElementsByTagName('link')[0]
but that is obviously not a generalized solution.
I can't make it work on a clean REPL. See it here.
Console returns _fbq is not a function
.
.
I realised that svelte-google-analytics has a better approach using async-script-loader and tried to "replace" Google Analytics by Facebook Pixel code, but I freeze in the callback()
part because Facebook's original code is way too obfuscated.
Hello
I tried to use this package with Svelte Kit with TS.
are there plans to add support to it ?
The errors I got.
fbq("Purchase", { currency, value });
getting this warning
fbq('Purchase', ...);" is not a valid fbq command.
ANyone know what that means? I was asked to add facebook page views tracking and conversion tracking on order success page.
Uncaught (in promise) TypeError: _fbq is not a function
FacebookPixel.svelte:73 Uncaught (in promise) TypeError: _fbq is not a function
at query (FacebookPixel.svelte:73)
at FacebookPixel.track (FacebookPixel.svelte:68)
at _layout.svelte:14
at run (index.mjs:18)
at Array.map (<anonymous>)
at index.mjs:1372
at flush (index.mjs:708)
at init (index.mjs:1458)
at new App (App.svelte:17)
at render (app.mjs:288)
<script>
import { goto, stores } from "@sapper/app";
import { onMount } from "svelte";
import FacebookPixel from "@beyonk/svelte-facebook-pixel";
import Navbar from "../components/Navbar.svelte";
import Footer from "../components/Footer.svelte";
import Banner from "../components/Banner.svelte";
const { page } = stores();
export let segment;
let fb;
onMount(() => {
fb.track("PageView");
});
</script>
<style>
main {
position: relative;
background-color: #0f1217;
padding: 0;
margin: 0 auto;
box-sizing: border-box;
color: #fff;
}
.mobile-buy-droids {
display: none;
}
@media only screen and (max-width: 768px) {
.mobile-buy-droids {
display: flex;
justify-content: flex-end;
position: fixed;
bottom: 0%;
width: 100%;
opacity: 1;
z-index: 2;
}
}
</style>
<Navbar {segment} />
<Banner />
<main>
{#if $page.path !== '/buy'}
<section class="mobile-buy-droids">
<a href="buy">
<img src="/images/droids-buy-now.png" alt="Buy Droids" />
</a>
</section>
{/if}
<slot />
</main>
<Footer {segment} />
<FacebookPixel bind:this={fb} id={FACEBOOK_TRACKING_ID} />
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.