0
votes

I am trying to create a custom dimension that enables me to view the total amount of ad revenue earned by individual authors on my website and for that, I need a custom dimension(Author). I currently have this code but I'm not sure where to insert/modify it to retrieve author data.

var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);

I'm insanely new to coding and tried following some tutorials but for some reason, the site isn't pulling the author's name. I've also set up google tag manager and everything but I find that I'm only stuck at the coding part. Should I add this code to the header of my site or do I need to modify the code above?

1
Where do you want to pull the author from? The best thing to do is get the information via Google Tag Manager and send it to Google Analytics. - Raoul Dundas
@RaoulDundas I'm not sure where I can pull author from. It's a Wordpress site with many authors, so I was wondering if there's a way to set that up as a custom dimension on GA. I've tried using google tag manager but I feel that the problem lies in the code on my site in retrieving author's name/data - dummy
If you don't know where you can pull the author from, how will you send the data to Google Analytics (GA)? Yes, there is a way to set it up as a custom dimension. First, you need to figure out which data the custom dimension should hold, t how you will retrieve the data and last how you will send it to GA. - Raoul Dundas
@RaoulDundas I've set up custom dimension for author on GA and GTM but somehow, it's not retrieving author so on my custom dashboard, anything beside author shows 0 - dummy
How did you set it up? - Raoul Dundas

1 Answers

0
votes

First, Locate the header.php of your theme in the source code (wp-content > themes > your theme). Next, copy and paste the following code after the opening head HTML tag ()

<!-- dataLayer -->
<script>
var dataLayer = window.dataLayer = window.dataLayer || [];

<?php if(is_single()){ ?>
<?php $post = get_post(); ?>
<?php $author_id = $post->post_author; ?>
<?php $author_fullname = get_the_author_meta( 'display_name', $author_id); ?>
dataLayer.push({
    'post_author_fullname': '<?php echo $author_fullname ?>',
    'event': 'PostInformation'
});
<?php } ?>
</script>
<!-- End dataLayer -->

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<YOUR-GTM-ID-HERE>');</script>
<!-- End Google Tag Manager -->

The code above initiates the dataLayer first and populates the information you need (in this case, the author on a blog post page). Next, Google Tag Manager is launched. The order is important, so do not change it. Note that you need to use your own GTM ID.

Second, you need to configure GTM to work with the information. Go to https://tagmanager.google.com and open your workspace.

On the left side, you'll see the main menu; click on the menu item Variables and click on the New button in the User-Defined Variables section.

Give the variable a name, choose the type Data Layer Variable, Name, and hit save to complete this step GTM User Defined dataLayer Variable

Third, create another User-Defined variable to hold your Google Analytics tracking ID. GTM User-Defined constant Variable to hold GA tracking ID

Fourth, create a new User-Defined variable to hold the Google Analytics Settings. GTM User-Defined Google Analytics Settings Variable

Make sure, the index corresponds with the index given in Google Analytics. Google Analytics custom dimension Author

In Google Tag Manager, configure the tag as follows. GTM Tag configuration

As you can see the info is being sent to GA: enter image description here