A bit of self promotion but I wrote an EE:U article about Consents at https://u.expressionengine.com/article/a-consent-module-primer
There are numerous ways to do this but what you could is create a consent for your tracking code and use that consent to allow code to be added.
Here’s a rough example with a consent called “ee:twitter”…
{!-- consent form tag --}
{if ! consent:has_responded:ee:twitter}
{exp:consent:form consent="ee:twitter"}
{!-- pass the consent value in a hidden input --}
{consents}
<input type="hidden" name="{consent_short_name}" value="y">
{/consents}
{!-- HTML --}
Allow Twitter surveillance scripts?
{!-- submit button--}
<button type="submit">Yes please!</button>
{/exp:consent:form}
{/if}
{!-- show the content after the form has been submitted --}
{exp:consent:requests consent="ee:twitter"}
{if consent_granted}
[YOUR-CODE-HERE]
{/if}
{/exp:consent:requests}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.