AMP extraUrlParams Custom GA Dimensions - google-analytics

I am trying to set custom variables on my AMP pages to use in GA Custom Dimensions. I have two clarifying questions I need help with
Question 1:
Using extraUrlParams do I use "cd1" or the variable name used in GA custom dimensions?
"cd1": "<?php echo $author; ?>"
or
"author": "<?php echo $author; ?>"
Question 2:
Do I need to set up these variables and a trigger in GTM? or does the extraUrlParams code take care of that?

Use "cd1" it should follow this: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cd_
That's if you're NOT USING GTM for GA on AMP.
How do you have GA implemented? If you implemented GA through GTM on your AMP pages, then you should use:
{
"vars": {
"nameofcd1author": "Patrick",
"nameofcd2title": "Patrick's article"
}
}
Then in GTM, create a AMP Variable and reference the keys in "vars", do this for all custom dimensions:
Then in your GA Tag in GTM. Configure the custom dimensions under "More settings" > "Custom Dimensions", the "Index" column is the number that refeers back to the index of the CD in GA like so:

Related

Show content if the post belongs to a specific author

I would like to know if there is any way to enable a script for visitors if the post they visit belongs to a specific author.
It is a script that I want to include in header.php or in single.php, if the visitor goes to a post that belongs to James, show or enable the following script in the header:
<script src="/wpauthors/james.js"></script>
function authScript() {
if(get_the_author_meta('ID' == 2) { // 2 is James uer ID here
echo '<script src="/wpauthors/james.js"></script>';
}
}
add_action('wp_head', 'authScript');
Add this to your functions.php. It should work. Haven't tested this myself.

GA not recording "Sessions with Add to Basket"

Summary
We have a Magento 2 store that we are having trouble setting up Google Analytics to track eccommerce correctly.
We have followed the Magento docs for setting up GA & GTM, but it is not collecting the correct amount of Sessions with Add to Basket in the Conversions > Ecommerce > Shopping Behavior.
From the image above you can see that it is not tracking the sessions correctly. 27 Sessions with Checkout but only 1 Session with Add to Basket, this does not add up, a user must add to cart before getting to the checkout.
Also, when I check the no of orders on Magento I see there are actually 29 orders, 27 of which are from different users. So it seems it's not capturing the number of orders correctly too.
Debugging/Setup
We followed the Magento docs for setting up GA & GTM as I show below. If you click on an image below you should see a better quality one.
We have E-commerce setup in GA:
At first I discovered with the GTM preview mode that the addToCart tag which was set up with the recommended Magento configuration was not firing. It seems the data layer event was not firing for the GTM trigger.
So I set up a new trigger which fired on Click and added it to the :
I can now see the tag firing in GTM preview mode:
And I can see the Add to Cart event in GAs Real-time Event report.
But still it's not showing the correct data in the Sessions with Add to Basket in the Conversions > Ecommerce > Shopping Behavior.
Questions
What could I be missing?
Should I set Non-Interaction Hit to false in the GTM tag setting?
Should I set use data layer to false for the tag in GTM, as the
addToCart custom event is not firing? Or maybe this is still needed
for something.
Any tips on how I can debug why the addToCart custom event is nor
firing on Magento while GTM is in preview mode?
I noted that the session in Magento is 3.5 hours, while in GA the Session timeout is only 30min. Perhaps this could be it? we changed the GA session timeout to match Magento's, and this was not it :(
Thank you in advance, any help appreciated :)
Based on the comment from #lossleader to my question, I was able to identify and fix the issue. I'll answer it here in case anyone else finds it helpful.
Basically as #lossleader said in his comment the main thing is:
The custom event should also have contained the e-commerce data for the use dataLayer tag.
So triggering the addToCart tag with a simple click event is not enough as it does not contain the e-commerce data, I needed to fix how the enhance ecommerce addToCart event was triggered on Magento.
I discovered that Magento Commerce changed/fixed how they end up calling the function which trigger this event in recent upgrades, i.e. commit MAGETWO-69210 & commit MAGETWO-87437
Our Magento did receive these changes as the list.phtml template and the catalog-add-to-cart.js file were being overridden in it's theme.
Updating the theme files as the below diff shows resolved the issue in our case:
diff --git a/app/code/Namespace/CategoryPages/view/frontend/templates/product/list.phtml b/app/code/Namespace/CategoryPages/view/frontend/templates/product/list.phtml
index 6771e863..2ab8905c 100644
--- a/app/design/frontend/Namespace/theme/Magento_Catalog/templates/product/list.phtml
+++ b/app/design/frontend/Namespace/theme/Magento_Catalog/templates/product/list.phtml
## -91,7 +91,7 ## $_helper = $this->helper('Magento\Catalog\Helper\Output');
<div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
<?php if ($_product->isSaleable()): ?>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
- <form data-role="tocart-form" action="<?php /* #escapeNotVerified */ echo $postParams['action']; ?>" method="post">
+ <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* #NoEscape */ $postParams['action'] ?>" method="post">
<input type="hidden" name="product" value="<?php /* #escapeNotVerified */ echo $postParams['data']['product']; ?>">
<input type="hidden" name="<?php /* #escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* #escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
<?php echo $block->getBlockHtml('formkey')?>
diff --git a/app/design/frontend/Namespace/theme/Magento_Catalog/web/js/catalog-add-to-cart.js b/app/design/frontend/Namespace/theme/Magento_Catalog/web/js/catalog-add-to-cart.js
index fae6f0fa..34978ec4 100644
--- a/app/design/frontend/Namespace/theme/Magento_Catalog/web/js/catalog-add-to-cart.js
+++ b/app/design/frontend/Namespace/theme/Magento_Catalog/web/js/catalog-add-to-cart.js
## -6,8 +6,10 ##
define([
'jquery',
'mage/translate',
+ 'underscore',
+ 'Magento_Catalog/js/product/view/product-ids-resolver',
'jquery/ui'
-], function ($, $t) {
+], function ($, $t, _, idsResolver) {
'use strict';
$.widget('mage.catalogAddToCart', {
## -75,7 +77,9 ## define([
* #param {String} form
*/
ajaxSubmit: function (form) {
- var self = this;
+ var self = this,
+ productIds = idsResolver(form),
+ formData = new FormData(form[0]);
$(self.options.minicartSelector).trigger('contentLoading');
self.disableAddToCartButton(form);
## -97,6 +101,13 ## define([
success: function (res) {
var eventData, parameters;
+ $(document).trigger('ajax:addToCart', {
+ 'sku': form.data().productSku,
+ 'productIds': productIds,
+ 'form': form,
+ 'response': res
+ });
+
if (self.isLoaderEnabled()) {
$('body').trigger(self.options.processStop);
}
The custom event that was pushed should also have contained the e-commerce data referred to in the "use dataLayer" tag, so you really needed to fix the magento configuration or detection of the user action rather than emulate it. I.e. look for customizations to the store's html around these inputs/forms and try reverting them to stock.
It looks like you found the complete answer by looking into updates to magento for the addToCart event that were being overridden by the installed theme.

Wordpress: add custom page attribute

I have the NextGEN Gallery plugin on my wordpress site. Normally I would add this short code in my page content to display gallery items:
[ nggallery id=5 template=custom ]
Now I'd like to replace this by adding custom fields in the Page Attributes setting when you are adding/editing a page. The custom fields would be "Gallery ID" and "Template name".
I'm of course using a custom page template. How can I retrieve the page attributes into this page template?
Thanks in advance!
Yes, what Stratboy said. Documentation here. This is the setup that should work for you:
<?php
$gallery_ID = get_post_meta($post->ID, 'Gallery ID', true);
$template_name = get_post_meta($post->ID, 'Template Name', true);
if ($gallery_ID && $template_name){
//echo '$gallery_ID: '.$gallery_ID.'; $template_name: '.$template_name.';';
echo do_shortcode('[nggallery id="'.$gallery_ID.'" template="'.$template_name.'"]');
}
?>
So, first:
have you already implemented the custom page template?
have you already implemented the custom fields?
Anyway, generally, in a template you get your custom fields values using the get_post_meta function in the Loop, like this:
//the last param tell if you want the value returned as a string (true) or an array (false)
get_post_meta($post->ID,'field name',true);
You can echo the returned value or use it for other tasks.
Let me know.

How to make a plugin to count visitors for posts under specific category

How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.

Adding a function to wordpress theme to bypass/replace premalinks

Looking at Cssremix.com
when you hover over a item you can see the "Views" function I'm assuming they are using a plugin that links with the post views but when clicked the item redirects not to the post but to a website/site used
This plugin gives you the post views. To filter a link you can hook into certain filters, such as post_link or the_permalink.
Here are some docs:
Plugin API usage
Filter reference
The usage would be something like this:
add_action('post_link', 'do_custom_link');
function do_custom_link($url, $post) {
$external_url = get_post_meta($post->ID, 'external_url', true);
if($external_url) {
return $external_url;
}
return $url;
}
This would get the external url from a meta field stored with the post, called external_url. You would define that meta field using the custom fields UI when you create the post through the admin pages.

Resources