Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active December 18, 2022 11:28
Show Gist options
  • Save finalwebsites/507173534d049d78c872eee8b3b342fc to your computer and use it in GitHub Desktop.
Save finalwebsites/507173534d049d78c872eee8b3b342fc to your computer and use it in GitHub Desktop.
WooCommerce code voorbeelden voor de integratie met Clicky Analytics
<?php
add_action('wp_footer', 'fw_get_user_for_clicky');
function fw_get_user_for_clicky() {
$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
// Vraag je klant/gebruiker eerst om toestemming voordat je deze code toont
echo '
<script type="text/javascript">
var clicky_custom = clicky_custom || {};
clicky_custom.visitor = {
username: "'.$current_user->display_name.'",
email: "'.$current_user->user_email.'"
};
</script>';
}
}
<?php
function clicky_log( $a ) {
/*
Deze functie is gebaseerd op de code van https://clicky.com/help/code/php-clicky-log.phps.
Let op! Je moet de volgende twee variabelen aanpassen ($site_id en $admin_site_key).
Beide kan je vinden via het instelling tabje van je website profiel in Clicky.
*/
$site_id = "xxx";
$sitekey_admin = "xxxxxx";
// Wijzig niet de code hieronder
$type = $a['type'];
if( !in_array( $type, array( "pageview", "download", "outbound", "click", "custom", "goal" ))) $type = "pageview";
$file = "http://in.getclicky.com/in.php?site_id=".$site_id."&sitekey_admin=".$sitekey_admin."&type=".$type;
if( isset($a['ref']) ) $file .= "&ref=".urlencode( $a['ref'] );
if( isset($a['ua']) ) $file .= "&ua=". urlencode( $a['ua'] );
if( isset($a['session_id']) && is_numeric( $a['session_id'] )) {
$file .= "&session_id=".$a['session_id'];
} else {
if( empty($a['ip_address']) ) $a['ip_address'] = $_SERVER['REMOTE_ADDR']; # haalt automatische het IP via PHP
if( !preg_match( "#^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$#", $a['ip_address'] )) return false;
$file .= "&ip_address=".$a['ip_address'];
}
// Goals kunnen als nummer of array worden opgegeven
if( isset($a['goal']) ) {
if( is_array( $a['goal'] )) {
foreach( $a['goal'] as $key => $value ) $file .= "&goal[".urlencode( $key )."]=".urlencode( $value );
} else {
$file .= "&goal[id]=".$a['goal'];
}
}
// Custom data wordt altijd als array opgegeven met key=>values indeling
if (isset($a['custom'])) {
foreach( $a['custom'] as $key => $value ) $file .= "&custom[".urlencode( $key )."]=".urlencode( $value );
}
if( $type == "goal" || $type == "custom" ) {
// doe hier niets
} else {
if( isset($a['href']) ) {
if( $type == "outbound" ) {
if( !preg_match( "#^(https?|telnet|ftp)#", $a['href'] )) return false;
} else {
# alle andere soorten acties / links
if( !preg_match( "#^(/|\#)#", $a['href'] )) $a['href'] = "/" . $a['href'];
}
$file .= "&href=".urlencode( $a['href'] );
}
if( isset($a['title']) ) $file .= "&title=".urlencode( $a['title'] );
}
return file( $file ) ? true:false;
}
<?php
add_action( 'woocommerce_thankyou', 'fws_add_content_thankyou' );
function fws_add_content_thankyou( $order_id ) {
$order = wc_get_order( $order_id );
$order_total = $order->get_total();
$goal_id = 12345; // vervang 12345 met de ID vanuit Clicky
echo '
<script type="text/javascript">
var clicky_goal = { id: "'.$goal_id.'", revenue: "'. $order_total.'" };
</script>';
}
<?php
add_action( 'woocommerce_add_to_cart', 'fw_track_add_to_cart_event');
function fw_track_add_to_cart_event() {
if (function_exists('clicky_log')) {
clicky_log(array('type' => 'goal', 'goal' => 12345)); // vervang 12345 met de ID vanuit Clicky
}
}
@finalwebsites
Copy link
Author

Plaats de bovenstaande functies/code (zonder de "<?php") in het functions.php file van je WordPress child theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment