Skip to content

Commit 525539e

Browse files
authored
Merge pull request #111 from mustachebash/feature/tiktok-pixel
added TikTok ads pixel for conversion tracking
2 parents 0a59600 + ea9cc54 commit 525539e

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/components/tickets.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ function updateCartQuantities() {
9393
updateSubtotals();
9494
}
9595

96+
async function sha256(str: string) {
97+
// Convert the string to a Uint8Array
98+
const encoder = new TextEncoder();
99+
const data = encoder.encode(str);
100+
101+
// Use the subtle crypto API to hash the data
102+
const hashBuffer = await window.crypto.subtle.digest('SHA-256', data);
103+
104+
// Convert the hash to a hex string
105+
const hashArray = Array.from(new Uint8Array(hashBuffer));
106+
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
107+
108+
return hashHex;
109+
}
110+
96111
const targetGuestId = (new URLSearchParams(location.search).get('targetGuestId')) ?? undefined;
97112
function completePurchase(nonce: string) {
98113
return fetch(API_HOST + '/v1/orders', {
@@ -183,6 +198,26 @@ function completePurchase(nonce: string) {
183198
} catch(e) {
184199
// Don't let FB break the site
185200
}
201+
202+
try {
203+
sha256(customer.email).then(hashedEmail => {
204+
(window as any).ttq.identify({
205+
// sha256 hash of the customer email address
206+
email: hashedEmail
207+
});
208+
(window as any).ttq.track('Purchase', {
209+
currency: 'USD',
210+
value: total,
211+
contents: cart.map(i => ({
212+
content_id: i.productId,
213+
content_type: products[i.productId].type,
214+
content_name: products[i.productId].name
215+
}))
216+
});
217+
}).catch(console.error);
218+
} catch(e) {
219+
// Don't let TikTok break the site
220+
}
186221
});
187222
}
188223

src/layouts/Layout.astro

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ const ogImageURL = openGraphImage ? new URL(openGraphImage.src, Astro.site).href
7575
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1354987751343916&ev=PageView&noscript=1" />
7676
</noscript>
7777
<!-- End Facebook Pixel Code -->
78+
79+
<!-- TikTok Pixel Code -->
80+
81+
<script is:inline>
82+
!function (w, d, t) {
83+
w.TiktokAnalyticsObject=t;var ttq=w[t]=w[t]||[];ttq.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie","holdConsent","revokeConsent","grantConsent"],ttq.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var i=0;i<ttq.methods.length;i++)ttq.setAndDefer(ttq,ttq.methods[i]);ttq.instance=function(t){for(
84+
var e=ttq._i[t]||[],n=0;n<ttq.methods.length;n++)ttq.setAndDefer(e,ttq.methods[n]);return e},ttq.load=function(e,n){var r="https://analytics.tiktok.com/i18n/pixel/events.js",o=n&&n.partner;ttq._i=ttq._i||{},ttq._i[e]=[],ttq._i[e]._u=r,ttq._t=ttq._t||{},ttq._t[e]=+new Date,ttq._o=ttq._o||{},ttq._o[e]=n||{};n=document.createElement("script")
85+
;n.type="text/javascript",n.async=!0,n.src=r+"?sdkid="+e+"&lib="+t;e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(n,e)};
86+
87+
ttq.load('D3CBULBC77UCTDLGROGG');
88+
ttq.page();
89+
}(window, document, 'ttq');
90+
</script>
91+
<!-- End TikTok Pixel Code-->
7892
</head>
7993

8094
<body>

0 commit comments

Comments
 (0)