Skip to content

Commit b4b80f4

Browse files
committed
Initial commit
0 parents  commit b4b80f4

File tree

13 files changed

+542
-0
lines changed

13 files changed

+542
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.DS_Store
2+
.thumbs.db
3+
node_modules
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Editor directories and files
9+
.idea
10+
.vscode
11+
*.suo
12+
*.ntvs*
13+
*.njsproj
14+
*.sln
15+
16+
dist

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Dreamonkey S.r.l.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# @dreamonkey/quasar-app-extension-tracking
2+
3+
This is a [Quasar App Extension (AE)](https://quasar.dev/app-extensions/introduction#Introduction)
4+
It provides composables for many commonly used tracking scripts using [Quasar Meta Composable](https://quasar.dev/vue-composables/use-meta).
5+
6+
- Google Tag Manager -> `useGoogleTagManager`
7+
- Facebook Pixel -> `useFacebookPixel`
8+
- Linkedin Insight Tag -> `useLinkedinInsightTag`
9+
10+
We'll gladly accept other contributions.
11+
12+
# Install
13+
14+
```bash
15+
quasar ext add @dreamonkey/tracking
16+
```
17+
18+
# Uninstall
19+
20+
```bash
21+
quasar ext remove @dreamonkey/tracking
22+
```
23+
24+
# Usage
25+
26+
Composables are meant to be placed into your layout components, as you usually want them to be triggered each time a user land on any of your pages.
27+
All composables accept an id/key, either as a plain string or as a ref.
28+
When using a ref, the script won't be added when its value is an empty string, undefined or any falsy value.
29+
30+
```ts
31+
// src/layouts/main-layout.vue
32+
33+
import {
34+
useGoogleTagManager,
35+
useLinkedinInsightTag,
36+
} from "@dreamonkey/quasar-app-extension-tracking";
37+
38+
export default {
39+
name: "MainLayout",
40+
setup() {
41+
// Add Google Tag Manager script
42+
useGoogleTagManager("GTM-XXXXXXX");
43+
44+
const linkedinId = ref();
45+
// Won't add the Linkedin Insight Tag script since the provided ref has undefined value
46+
useLinkedinInsightTag(linkedinId);
47+
48+
// Tracking AE will react to the change and add Linkedin Insight Tag script
49+
linkedinId.value = "XXXXXXX";
50+
},
51+
};
52+
```
53+
54+
# Similar work
55+
56+
- https://github.com/gtm-support/vue-gtm: much more complete on GTM side, we may copy over some features at some point
57+
58+
# Donate
59+
60+
If you appreciate the work that went into this App Extension, please consider [donating to Dreamonkey](https://github.com/sponsors/dreamonkey).

package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "@dreamonkey/quasar-app-extension-tracking",
3+
"version": "0.1.0",
4+
"description": "Provide composables to add multiple tracking scripts via Quasar Meta plugin",
5+
"keywords": [
6+
"quasar",
7+
"tracking",
8+
"facebook",
9+
"pixel",
10+
"google",
11+
"analytics",
12+
"tag manager",
13+
"linkedin",
14+
"insight tag"
15+
],
16+
"author": "Paolo Caleffi <p.caleffi@dreamonkey.com>",
17+
"license": "MIT",
18+
"main": "dist/exports.js",
19+
"types": "dist/exports.d.ts",
20+
"scripts": {
21+
"build": "rimraf dist && tsc --declaration",
22+
"install-build-clean": "yarn install && yarn build && rimraf node_modules",
23+
"test": "echo \"No test specified\" && exit 0",
24+
"deploy": "yarn build && yarn publish --tag latest"
25+
},
26+
"files": [
27+
"dist",
28+
"src"
29+
],
30+
"repository": {
31+
"type": "git",
32+
"url": "https://github.com/dreamonkey/quasar-app-extension-tracking"
33+
},
34+
"bugs": "https://github.com/dreamonkey/quasar-app-extension-tracking/issues",
35+
"homepage": "",
36+
"devDependencies": {
37+
"@babel/types": "^7.15.6",
38+
"@types/node": "^12.20.27",
39+
"quasar": "^2.6.6",
40+
"rimraf": "^3.0.2",
41+
"typescript": "^4.6.4",
42+
"vue": "^3.2.33"
43+
},
44+
"peerDependencies": {
45+
"quasar": "^2.0.0",
46+
"vue": "^3.2.0"
47+
},
48+
"engines": {
49+
"node": ">= 12.2.0",
50+
"npm": ">= 5.6.0",
51+
"yarn": ">= 1.6.0"
52+
},
53+
"publishConfig": {
54+
"access": "public"
55+
}
56+
}

src/exports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./google";
2+
export * from "./linkedin";
3+
export * from "./facebook";

src/facebook.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TrackingId, useTrackingMeta } from "./shared";
2+
3+
export function useFacebookPixel(maybeId: TrackingId) {
4+
useTrackingMeta(maybeId, (id) => ({
5+
script: {
6+
facebookPixel: {
7+
type: "text/javascript",
8+
innerHTML: `!function(f,b,e,v,n,t,s)
9+
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
10+
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
11+
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
12+
n.queue=[];t=b.createElement(e);t.async=!0;
13+
t.src=v;s=b.getElementsByTagName(e)[0];
14+
s.parentNode.insertBefore(t,s)}(window,document,'script',
15+
'https://connect.facebook.net/en_US/fbevents.js');
16+
fbq('init', '${id}');
17+
fbq('track', 'PageView');`,
18+
},
19+
},
20+
noscript: {
21+
facebookPixel: `<img height="1" width="1" src="https://www.facebook.com/tr?id=${id}&ev=PageView&noscript=1"/>`,
22+
},
23+
}));
24+
}

src/google.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { TrackingId, useTrackingMeta } from "./shared";
2+
3+
export function useGoogleTagManager(maybeId: TrackingId) {
4+
useTrackingMeta(maybeId, (id) => ({
5+
script: {
6+
googleTagManager: {
7+
type: "text/javascript",
8+
innerHTML: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
9+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
10+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
11+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
12+
})(window,document,'script','dataLayer','${id}');
13+
`,
14+
},
15+
},
16+
noscript: {
17+
googleTagManager: `
18+
<iframe src="https://www.googletagmanager.com/ns.html?id=${id}"
19+
height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
20+
},
21+
}));
22+
}

src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Quasar App Extension index/runner script
3+
* (runs on each dev/build)
4+
*
5+
* Docs: https://quasar.dev/app-extensions/development-guide/index-api
6+
* API: https://github.com/quasarframework/quasar/blob/master/app/lib/app-extension/IndexAPI.js
7+
*/
8+
9+
module.exports = function (api) {
10+
api.extendQuasarConf((conf) => {
11+
conf.framework.plugins.push("Meta");
12+
conf.build.transpileDependencies.push(
13+
/@dreamonkey[\\/]quasar-app-extension-tracking/
14+
);
15+
});
16+
};

src/linkedin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { TrackingId, useTrackingMeta } from "./shared";
2+
3+
export function useLinkedinInsightTag(maybeId: TrackingId) {
4+
useTrackingMeta(maybeId, (id) => ({
5+
script: {
6+
linkedinInsightTag: {
7+
type: "text/javascript",
8+
innerHTML: `window._linkedin_data_partner_ids = window._linkedin_data_partner_ids || [];
9+
window._linkedin_data_partner_ids.push(${id});
10+
(function(){var s = document.getElementsByTagName("script")[0];
11+
var b = document.createElement("script");
12+
b.type = "text/javascript";b.async = true;
13+
b.src = "https://snap.licdn.com/li.lms-analytics/insight.min.js";
14+
s.parentNode.insertBefore(b, s);})();`,
15+
},
16+
},
17+
noscript: {
18+
linkedinInsightTag: `<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=${id}&fmt=gif" />`,
19+
},
20+
}));
21+
}

src/shared.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useMeta } from "quasar";
2+
import { MetaOptions } from "quasar/dist/types/meta";
3+
import { Ref, unref } from "vue";
4+
5+
export type TrackingId = string | Ref<string | undefined>;
6+
7+
export function useTrackingMeta(
8+
maybeId: TrackingId,
9+
metaOptionsFn: (id: string) => MetaOptions
10+
): void {
11+
useMeta(() => {
12+
const id = unref(maybeId);
13+
if (!id) {
14+
return {};
15+
}
16+
17+
return metaOptionsFn(id);
18+
});
19+
}

0 commit comments

Comments
 (0)