Integrating Leaky Paywall with Google Analytics
There are many ways to integrate Leaky Paywall with Google Analytics. We'll start with event tracking. This assumes you using the new gtag syntax for your Google Analytics tracking code.
1. Event: Subscribe nag display
Add this code into the "Subscribe or Login Message" setting under Leaky Paywall > Settings > General.
<script>gtag('event', 'view_item', { 'event_category': 'engagement', 'event_label': 'subscribe_nag', });</script>
You can also add an event to the Upgrade Nag
<script>gtag('event', 'view_item', { 'event_category': 'engagement', 'event_label': 'upgrade_nag', });</script>
2. Event: Subscribe nag subscribe click
Add this code into the "Subscribe or Login Message" setting under Leaky Paywall > Settings > General.
<a onClick="gtag('event', 'click', { event_category: 'subscribe_nag', event_action: 'click', event_label: 'subscribe'});" href="{{SUBSCRIBE_URL}}">Subscribe</a><br> <br>
3. Add a Leaky Paywall transaction to the data layer
If you are using Google Tag Manager, you can use the following snippet to add Leaky Paywall transaction data to the data layer.
Note: If you are having trouble, please set up debug mode according to this doc:
https://developers.google.com/analytics/devguides/collection/ga4/set-up-ecommerce
<?php | |
add_action( 'wp_footer', 'zeen101_add_transaction_event_code' ); | |
function zeen101_add_transaction_event_code() { | |
if ( ! isset( $_GET['lp_txn_id'] ) ) { | |
return; | |
} | |
$txn_id = absint( $_GET['lp_txn_id'] ); | |
$price = get_post_meta( $txn_id, '_price', true ); | |
$level_id = get_post_meta( $txn_id, '_level_id', true ); | |
$level = get_leaky_paywall_subscription_level( $level_id ); | |
?> | |
<script> | |
gtag("event", "purchase", { | |
transaction_id: "<?php echo $txn_id; ?>", | |
value: <?php echo $level['price']; ?>, | |
currency: "USD", | |
items: [ | |
{ | |
item_id: "<?php echo $level_id; ?>", | |
item_name: "<?php echo $level['label']; ?>", | |
affiliation: "Your Publication", | |
index: 0, | |
item_brand: "Your Publication", | |
item_category: "Subscription", | |
price: <?php echo $level['price']; ?>, | |
quantity: 1 | |
}, | |
}] | |
}); | |
</script> | |
<?php | |
} |