How to add a tag on an action

The easiest way to trigger custom events from other plugins in the funnel builder is to add a tag whenever an action is completed.

This acts as a super easy alternative to creating a custom benchmark.

The Plugins API is really easy to hook into and doesn’t require a ton of custom development.

Version 2.0 or later

<?php

add_action( 'purchased_a_product', 'add_a_tag_when_purchasing', 10, 2 );

/**
 * @param $customer Customer
 * @param $order Order
 */
function add_a_tag_when_purchasing( $customer, $order )
{
	
	$tags_to_add = [ 'Purchased Product', 1, 17, 'other-tag' ]; // You can use any combination of text or tag Ids
	
	// There are several ways to retrieve a contact record at this point, choose the most relevant one.
	
	// Option A: Use the tracking cookie.
	
	$contact = \Groundhogg\Plugin::$instance->tracking->get_current_contact();
	
	if ( ! $contact ){
		return;
	}
	
	$contact->apply_tag( $tags_to_add );
	
	// Option B: Create a new contact record, or retrieve data from passed variables
	
	$email = $customer->email;
	$first_name = $customer->first_name;
	
	$contact = new \Groundhogg\Contact( [   // This will retrieve any existing contact with the given email,
		'first_name' => $first_name,        // If none exists a new record will be created.
		'email' => $email
	] );
	
	$contact->apply_tag( $tags_to_add );
	
}

Version 1.3 up to 2.0

<?php 

add_action( 'purchased_a_product', 'add_a_tag_when_purchasing_old', 10, 2 );

/**
 * @param $customer Customer
 * @param $order Order
 */
function add_a_tag_when_purchasing_old( $customer, $order )
{

	$tags_to_add = [ 'Purchased Product', 1, 17, 'other-tag' ]; // You can use any combination of text or tag Ids

	// There are several ways to retrieve a contact record at this point, choose the most relevant one.

	// Option A: Use the tracking cookie.

	$contact = WPGH()->tracking->get_contact();

	if ( ! $contact ){
		return;
	}

	$contact->apply_tag( $tags_to_add );

	// Option B: Create a new contact record, or retrieve data from passed variables

	$email = $customer->email;
	$first_name = $customer->first_name;
	
	$id = WPGH()->contacts->add( [
		'first_name' => $first_name,        // If none exists a new record will be created.
		'email' => $email
	] );
	
	if ( ! $id ){
		return;
	}
	
	$contact = wpgh_get_contact( $id );
	
	$contact->apply_tag( $tags_to_add );

	// This will update their tracking cookie using any new relevant information.
	wpgh_after_form_submit_handler( $contact );
}
Was this article helpful to you? Yes No

How can we help?