### Installing PythonMonkey via pip
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index.html
This command installs the PythonMonkey library using pip, Python's package installer. It is the first step to set up PythonMonkey in your development environment, making the 'pythonmonkey' package available for import.
```Shell
pip install pythonmonkey
```
--------------------------------
### Evaluating JavaScript in Python with PythonMonkey
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index.html
This snippet demonstrates how to import PythonMonkey as 'pm' and execute a JavaScript string using the 'pm.eval()' function. The result, a JavaScript string converted to uppercase, is then printed in Python, showcasing basic interoperability between the two languages.
```Python
import pythonmonkey as pm
hello = pm.eval(" 'Hello World'.toUpperCase(); ")
print(hello)
```
--------------------------------
### Managing GDPR Consent and Google Analytics Tracking in JavaScript
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index-old.html
This JavaScript snippet handles user consent for tracking via Google Analytics. It dynamically loads the Google Analytics script if consent is given, stores the user's preference in local storage, and updates the UI accordingly. It also includes a fallback for navigation on body click.
```JavaScript
'use strict';
const gtagHTML = ` `;
function setTracking(allow) {
document.body.setAttribute('gdpr-query', 'complete');
window.localStorage.setItem('gdpr-query', JSON.stringify(allow));
window.setTimeout(() => {
console.log('allow-tracking=', allow);
if (allow) {
const gaScript = document.createElement('SCRIPT');
gaScript.setAttribute('type', 'text/javascript');
gaScript.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=G-F6ZF7J18CR');
gaScript.onload = function gaScript$onload() {
console.debug('loaded google analytics');
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-F6ZF7J18CR');
};
gaScript.onerror = console.error;
document.body.appendChild(gaScript);
}
document.body.onclick = () => { window.location.href = document.querySelector('A.no-show').getAttribute('href') };
}, 30);
}
if (window.localStorage.getItem('gdpr-query')) {
const trackingAllowed = JSON.parse(window.localStorage.getItem('gdpr-query'));
if (trackingAllowed) setTracking(true);
}
```
--------------------------------
### Checking Initial GDPR Consent on Load - JavaScript
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index.html
This code block executes on page load to check if GDPR consent has been previously stored in the browser's local storage. If consent was given, it calls the 'setTracking' function to enable Google Analytics.
```JavaScript
if (window.localStorage.getItem('gdpr-query')) {
const trackingAllowed = JSON.parse(window.localStorage.getItem('gdpr-query'));
if (trackingAllowed) setTracking(true);
}
```
--------------------------------
### Setting Google Analytics Tracking Consent - JavaScript
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index.html
This function handles the user's consent for Google Analytics tracking. It updates a 'gdpr-query' attribute on the document body, stores the consent status in local storage, and conditionally loads the Google Analytics script if tracking is allowed.
```JavaScript
function setTracking(allow) {
document.body.setAttribute('gdpr-query', 'complete');
window.localStorage.setItem('gdpr-query', JSON.stringify(allow));
window.setTimeout(() => {
console.log('allow-tracking=', allow);
if (allow) {
const gaScript = document.createElement('SCRIPT');
gaScript.setAttribute('type', 'text/javascript');
gaScript.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=G-F6ZF7J18CR');
gaScript.onload = function gaScript$onload() {
console.debug('loaded google analytics');
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-F6ZF7J18CR');
};
gaScript.onerror = console.error;
document.body.appendChild(gaScript);
}
}, 30);
}
```
--------------------------------
### Defining Google Analytics HTML Snippet - JavaScript
Source: https://github.com/distributive-network/pythonmonkey.io/blob/main/index.html
This constant defines a single-line HTML string containing the Google Analytics gtag.js script tags. It is intended for dynamic insertion into the DOM once user consent for tracking is granted.
```JavaScript
const gtagHTML = `