Im trying to track an event that has 3 dimensions (user id, widget id,post id). Every click event must have its own row with a unique post id, user id and widget id so i can track the number of clicks per post. Extra: All the dimensions are set to have a scope of 'user'.
ga('send', 'event', 'widget', 'click', 'uwp',
{
'dimension1': $user_id,
'dimension2': $widget_id,
'dimension3': $post_id
}
);
I later query using the reporting api.
$result = Analytics::query(
$start_date,
$end_date,
'ga:totalEvents',
array(
'dimensions' => 'ga:dimension3,ga:pagePath,ga:date,ga:eventAction',
'sort' => '-ga:date',
'filters' => 'ga:eventAction==click;ga:eventLabel==uwp;ga:dimension1=='.$user->id,
'max-results'=> '100'
));
The result is incorrect all the clicks are being bundled up into 1 dimension, being dimension3 the post id.
Ex) If i click on a post of id 30 (dimension3 is set to 30) and a post of id 10 it will report that post of id 30 has 2 clicks or some older id has 2 clicks.
Google Analytics Dashbaord Picks:
As you can see from the pick i have 1 row with post id of 2 and 7 clicks, yet i sent events with many different post ids. (dimension3 == post id).
The problem here is that you are setting all of these dimensions to have a scope of user. Because of this, each dimension can only have one value per user. This is fine for the user id dimension, but for a dimension like post id, this is likely incorrect. Presumably, a user can view many post ids. Setting post id to user scope will cause the post id value to be overwritten with the latest post each time a new post is seen (which is why only one value is appearing in your report). You'll instead want to set post id to have a scope of hit, which will allow a different value to be sent on each event hit.
You can change the scope via the interface.
Read more about custom dimension scope.
Related
CONTEXT:
I’ve used the WPForms plugin in Wordpress to create a custom donation form for my client. The client wants to accept donations via PayPal.
PROBLEM:
WPForms has a PayPal add-on feature but it costs $200.
QUESTION
To avoid paying $200 for the expensive plugin, how can I manually direct/post the WPForm data entered by the user (name, email, address, donation amount, and employment info) to PayPal?
In the example code you’ll see below, we’re first checking the form ID to make sure it matches the form that’s being targeted. Then we’re checking a specific field (by the field ID) to see if it’s empty.
Just remember to change the form ID from 5 to match your form ID and change the '4' to match your field ID.
function wpf_dev_process( $fields, $entry, $form_data ) {
// Optional, you can limit to specific forms. Below, we restrict output to
// form #5.
if ( absint( $form_data['id'] ) !== 5 ) {
return $fields;
}
// check the field ID 4 to see if it's empty and if it is, run the error
if(empty($fields[4]['value']))
{
// Add to global errors. This will stop form entry from being saved to the database.
// Uncomment the line below if you need to display the error above the form.
// wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Check the field ID 4 and show error message at the top of form and under the specific field
wpforms()->process->errors[ $form_data['id'] ] [ '4' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Add additional logic (what to do if error is not displayed)
}
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );
I want to accomplice the following. I want to automatically send an email on post publish/update when a certain post field value has changed.
In a post
A ACF field that has 4 options, lets say [ 'draft', 'ready for group1', 'ready for group 2', 'ready']
If this field gets changed on post update send email to "this" email address.
I think I need to know 2 things for this.
- How and where (what action) do I need to insert custom code on post publish/update
- How to compare new post data to old state (and is this possible/availible in the action above)
You can hook onto acf/save_post for this purpose. Read the documentation here:
https://www.advancedcustomfields.com/resources/acf-save_post/
Since you want the callback to fire before the values are stored, in order to compare the old value against the new one, keep in mind to add a priority of less than 10. Assuming the field with 4 options has the field key field_4afd4af14415f:
function on_acf_post_save($post_id) {
$post_type = get_post_type($post_id);
if ($post_type === 'your-post-type') {
$old_val = get_field('field_4afd4af14415f', $post_id);
$new_val = $_POST['acf']['field_4afd4af14415f'];
if ($old_val != $new_val) {
// Send desired mail in here:
// wp_mail(...);
}
}
}
add_action('acf/save_post', 'on_acf_post_save', 5, 1); // priority = 5
If your ACF field isn't at the top level, but inside a Group or Repeater, you will have to adapt the code reading from the $_POST['acf'] and get_field() result.
There's an action called 'woocommerce_checkout_create_order_line_item' where you can use the method 'add_meta_data(meta_key, meta_value)' to add meta data to the order item.
Every example did use this syntax: add_meta_data(__('Some key name', 'woocommerce'), $value);
This is perfect as long as the admin and the customer have set the same language and the field is only informal.
But I have to process the field programmatically later. So the key must always be the same, no matter which language is used.
So I use a simple string, but it won't be translated in the admin order area, on the Thank-You page, in the shopping cart etc. anymore.
How can I override the automatic output of the order item meta data? I've tried to hook i.e. into 'woocommerce_before_order_itemmeta($item_id, $item, $product)' (Admin order page), but there I can only delete $item->delete_meta_data('my_key'), I cannot add temporary meta keys => $item->add_meta_data(__('Translated key'), $value);
I also tried:
$item->set_meta_data(array('id' => 0, 'key' => __('Translated key'), 'value' => 'test123'));
print_r($item->get_meta_data());
Thanks!
There is no need to do the translation in the meta key.
What you could do is use the hook woocommerce_order_item_display_meta_key. There you can catch your key and output a tranlation for display.
Also you can filter keys to not display all custom meta keys if you have some keys that should not show up in the order details, but are for internal use only. This can be done with woocommerce_hidden_order_itemmeta
Here is a good post on working with order item meta data: https://www.ibenic.com/manage-order-item-meta-woocommerce/
How to track a Registration Form fields.
I have a "registration form" in my website. As usual we have common field like name, email id, address etc. Along with these we have two fileds which will have drop down menu to differentiate the users.
One Field name is Industry Type, this will have drop down menu like Health Care, Retail, Restaurants etc
Another one is User Type, this will have drop down menu like Merchant, Retails etc...
And all these users will have the same page after login.
Now i need to pass the Industry Type and User Type values to Analytics which the user selected while regestering.
Custom dimensions
In Google Analytics -> Tab Administration -> Tab Property -> Tab Custom Definitions -> Custom Dimensions
Create New Industry Type
Scope User
ID 1
Create New User Type
Scope User
ID 2
Tracking Code
ga("set","dimension1","Heavy Industry");
ga("set","dimension2","Super Customer");
OR
ga('send', 'event', 'Registration', 'New Member', {
'dimension1': "Heavy Industry",
'dimension2':'Super Customer'
});
OR TagManager
Create two dataLayer macros for industryType and userType
dataLayer.push({
'eventCategory':'Registration',
'eventAction':'New Member',
'industryType':'Heavy Industry',
'userType':'Super Customer',
'event':'fire'
});
I'm using the new Google Analytics Measurement Protocol and running into a problem where all my events are coming is as "new visitor" even though I'm sending the same client ID for all of the requests.
I assume Google is using the client ID to determine if a visitor is new or not, but it appears to be ignoring this value.
Here's the variables I'm sending in the POST request:
Array
(
[v] => 1
[tid] => *OBFUSCATED*
[cid] => E19A0922-ABDF-D704-E4D4-620FB7AD7885
[t] => event
[sr] => 1280x720
[ec] => Video%20Plays
[ea] => Sample
[el] => Roku%20App
[ev] => 1
)
Other events are similar, where the cid remains the same but the event category and values change.
It looks like after waiting a full day and sending some events again, they are showing as returning visits.
Your cid is wrong I guess. it should be a 32bit id separated from a dot. Find a actual case study in my blog post http://www.niroshan-samuel.com/google-universal-analytics-isnt-linking-offline-and-online-user-properly/