Skip to content

Instantly share code, notes, and snippets.

@fewlme
fewlme / full_js_hatch_widget.js
Last active November 6, 2023 08:32
FULL JS implementation for the Hatch widget
const sku = document.querySelector('.sku'); //Change it to the DOM element containing your SKU
const h_config = '653bc46459534829ab5f85d8'; //Change you config ID
if(sku !== undefined){
const mpn = sku.innerText;
const h_button = document.querySelector('.cart'); //Change '.cart' to the reference element where you want the button on your page
if(h_button !== undefined){
h_button.insertAdjacentHTML(
"afterend", //change based on where you want the button to show
'<span style="visibility: hidden; display: inline-block;" data-target-node-selector=".result-gh-'+h_config+'" data-gh-buy-now-button="" data-gh-mpn-code="'+mpn+'" data-gh-ean-code="" data-gh-country-code="NL" data-gh-endpoint-wtbo="gethatch.com/wtbonline/affiliate" data-gh-configuration-id="'+h_config+'"> Buy now </span>',
);
@fewlme
fewlme / gethatch.com-GA-event-widget.js
Last active January 3, 2024 16:42
Add GA event tracking on widget retailer links
//Please ask Hatch to provide a list of all retailer IDs vs names
const retailer_mapping = {
94349 : 'MSI Store',
89105 : 'Walmart',
};
//Listener on any hatch links
document.addEventListener( "click", hatchRetailerWidgetClick );
function hatchRetailerWidgetClick(ev){
const el = ev.target;
@fewlme
fewlme / gethatch.com-urls-utms_tracker.js
Last active September 2, 2021 11:59
Add UTMs and CID (created from the UTMs) values to hardcoded gethatch.com URLs in the DOM
/* ***************************************************
-- UTMS/CID to gethatch.com URLs replacer --
- To be placed before the closing body tag
*************************************************** */
const hatch_links = document.querySelectorAll("a[href^='https://gethatch.com']");
const utm_param_sorting = ['utm_campaign','utm_source','utm_medium','utm_content','utm_term'];
let aid = '';
(function() {
const params = new URLSearchParams(window.location.search);
const utm_params = [];
@fewlme
fewlme / gethatch.com-urls-tracker.js
Last active December 15, 2021 08:16
Add CID values to hardcoded gethatch.com URLs in the DOM
/* ***************************************************
-- CID to gethatch.com URLs replacer --
- To be placed before the closing body tag
*************************************************** */
const hatch_links = document.querySelectorAll("a[href^='https://gethatch.com']");
function getHatchCid() {
var url = window.location.href;
var regex = new RegExp("[?&#]cid(=([^&#]*)|&|#|$)","i");
var results = regex.exec(url);
if (!results) return null;
@fewlme
fewlme / cid_extractor.js
Created July 16, 2021 09:13
Standalone client side CID extractor
/* ***************************************************
-- Hatch CID tracking & storage --
- To be placed anywhere on the page
- Make sure all your gethatch.com links are place in a identifiable container
- Change this identifiable container at the end of the script
*************************************************** */
const addHatchParam = function addHatchParam(sParam,sCtnr) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
@fewlme
fewlme / hatch-utm-cid-tracking-storage.js
Last active January 17, 2023 17:16
Hatch UTMs and CID tracking & storage
/* ***************************************************
-- Hatch UTMs and CID tracking & storage --
- To be placed before the HATCH WIDGET LIBRARY script
*************************************************** */
const HATCH_TRACKING = "h_cidutm"; //cookie name
const HATCH_TRACKING_DURATION = 30; //time in days
const SET_COOKIE = true; //set cookie or not
let h_stored_cook = [];
let h_stored_value = '';
@fewlme
fewlme / execute_once_mounted.js
Last active March 1, 2023 09:29
Load Hatch JS in Vue/React/etc setups
/* ********************************************************************************
Please add this script once the application is mounted/loaded (e.g. HTML has been loaded, componentDidMount/useEffect in React, etc)
********************************************************************************* */
let HatchScript = document.createElement('script');
HatchScript.setAttribute('src', 'https://cdn.gethatch.com/hatch-buy-now-request-1.0.stable.js');
document.body.appendChild(HatchScript);
HatchScript.onload = function () {HatchRequest.init()};
@fewlme
fewlme / utms-to-cid.js
Last active May 11, 2022 08:30
Grabs the UTMs from the URL and transform them into a unique CID store in a cookie and injected in Hatch widget
/* ***************************************************
-- CID to data-gh-cid --
- To be placed before the HATCH WIDGET LIBRARY script
*************************************************** */
const HATCH_CID_COOKIE = "h_cid";
function createHatchCid() {
var url = window.location.href;
var regexp = /(?!&)utm_[^=]*=[^&]*(?=&)?/g;
var utms = url.match(regexp);
if(!utms) return null;
@fewlme
fewlme / hatch-cid.js
Last active March 24, 2023 07:58
Store the CID from URL in a cookie and inject it into Hatch Widget
/* ***************************************************
-- CID to data-gh-cid --
- To be placed before the HATCH WIDGET LIBRARY script
*************************************************** */
const HATCH_CID_COOKIE = "h_cid";
function getHatchCid() {
var url = window.location.href;
var regex = new RegExp("[?&#]cid(=([^&#]*)|&|#|$)","i");
var results = regex.exec(url);
if (!results) return null;
@fewlme
fewlme / hatch-utms.js
Last active June 11, 2021 09:11
Store UTMs from URL into a cookie and inject them into Hatch widget
/* ***************************************************
-- UTMs to data-extra-parameter-requests --
- To be placed before the HATCH WIDGET LIBRARY script
*************************************************** */
const HATCH_UTMS_COOKIE = 'h_utms', utmParams = ['utm_source','utm_medium','utm_campaign','utm_content','utm_term'];
let h_utms = {}, h_utms_v = '';
let utmQuery = decodeURIComponent(window.location.search.substring(1)),
utmVariables = utmQuery.split('&'),
ParameterName,
i;