Skip to content

Instantly share code, notes, and snippets.

@abhagsain
Last active March 17, 2023 07:58
Show Gist options
  • Save abhagsain/7f08b1fc005bb6e75c0cf64e9eaaa0f8 to your computer and use it in GitHub Desktop.
Save abhagsain/7f08b1fc005bb6e75c0cf64e9eaaa0f8 to your computer and use it in GitHub Desktop.
Ignore Rewardful affiliate tracking if the user came from Google Ads
import { Head, Html, Main, NextScript } from "next/document";
export default function Document(props) {
let pageProps = props.__NEXT_DATA__?.props?.pageProps;
return (
<Html
className="h-full scroll-smooth bg-white antialiased [font-feature-settings:'ss01']"
lang='en'
>
<Head>
{/* Only this script 👇 is important, ignore rest of the code */}
<script
type='text/javascript'
dangerouslySetInnerHTML={{ __html: process.env.rawJsFromFile }}
></script>
</Head>
<body className='flex h-full flex-col'>
<Main />
<NextScript />
</body>
</Html>
);
}
// Credit @michaelkoper - https://twitter.com/michaelkoper/status/1633836513359441924
class Affiliates {
constructor() {
const urlParams = new URLSearchParams(window.location.search)
if (urlParams.has('via') && !urlParams.get('gclid')) {
const script = document.createElement('script')
script.src = 'https://r.wdfl.co/rw.js'
script.setAttribute('async', '')
script.setAttribute('data-rewardful', '_ADD_YOUR_AFFILIATE_ID_HERE')
document.head.appendChild(script)
}
}
}
window.addEventListener('DOMContentLoaded', () => {
new Affiliates()
})
// Ignore these two files if you're not using next.js
// next.config.js
const fs = require("fs");
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
module.exports = withBundleAnalyzer({
env: {
rawJsFromFile: fs.readFileSync("./Affiliates.js").toString(),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment