Google Analytics: Javascript for linking custom dimension(author) to my website

374 views Asked by At

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

There are 1 answers

0
Raoul Dundas On

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