Tracking User Copy Text Behavior in Google Analytics 4 | BCS Tracking User Copy Text Behavior in Google Analytics 4 | BCSBCS

Tracking User Copy Text Behavior in Google Analytics 4

Google Analytics BCS 5 years ago (2019-12-18) 3348 Views 2 Comments

Updated: January 12, 2025

This time I will share how to use GTM to track user copying behavior.

The principle is to inject javascript through custom HTML to monitor whether the user has copying behavior. If so, send data through dataLayer.push, and the copied text and text length are passed through data layer variables and sent to GA4.

Step 1 : Custom HTML to inject javascript

In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Custom HTML」,Name it “HTML-Copy”, and make the following settings:Tracking User Copy Text Behavior in Google Analytics 4

Source Code:

<span style="font-size: 12pt;"><script>
// Declare function to get selected text from document
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

// Declare function on copy event
document.addEventListener("copy", function(e){
  dataLayer.push({
    "event": "textCopied",
    "clipboardText": getSelectionText(),
    "clipboardLength": getSelectionText().length
  });
});
</script>
</span>

If you want to track only copied emails or phone numbers, you can make some restrictions and add a judgment to send an event only when the copied text is equal to a specific value.

Step 2 : Set Up the Trigger

In GTM , click「Triggers」——「New」——「Choose a trigger type to begin setup…」——「Custom Event」,Name it “textCopied”, and make the following settings:

Tracking User Copy Text Behavior in Google Analytics 4

 

Step 3 : Set Up the DataLayer Variables

In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Data Layer Variable」,Name it “DL – Clipboard Text”, and make the following settings: Tracking User Copy Text Behavior in Google Analytics 4

Similarly, set DL – Clipboard Length:Tracking User Copy Text Behavior in Google Analytics 4

Step 4 : Set Up the Tags

In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Google Analytics: GA4 Event」,Name it “GA4-Event-TextCopy”, and make the following settings:Tracking User Copy Text Behavior in Google Analytics 4

The event name is TextCopy

Two event parameters TextCopy_text and TextCopy_length are defined to pass the copied content and content length.

Step 5 : Preview and Publish

Next is preview debugging.

Tracking User Copy Text Behavior in Google Analytics 4

You can see that Tags is triggered. Next, check whether the values ​​of the event parameters are correct:

Tracking User Copy Text Behavior in Google Analytics 4

 

The values ​​of the event parameters are correct and can be published.

 

Step 6 : Custom Definitions

We have set the event parameters TextCopy_text and TextCopy_length. Next, we need to register the event parameters in GA4

  • TextCopy_text is event scope custom dimensions
  • TextCopy_length is event scope custom metrics

In GA4,click「Admin」——「Custom definitions」——「Create custom dimension」, then do the following configuration:

Tracking User Copy Text Behavior in Google Analytics 4

 

In GA4,click「Admin」——「Custom definitions」——「Custom metrics」——「Create custom metrics」, then do the following configuration:Tracking User Copy Text Behavior in Google Analytics 4

Read More: GA4 Event Tracking Series ⓪ — Rules and Limits for GA4 Events

Step 7 : Verify Data in Explore

Generally, after 24 hours, you can see the data in GA4, such as:Tracking User Copy Text Behavior in Google Analytics 4


If you don't understand, You can leave a comment below.
Like (3)
Post my comment
Cancel comment
Expression Bold Strikethrough Center Italic

Hi, you need to fill in your nickname and email!

  • Nickname (required)
  • Email (required)
  • Website
(2) friends in the comments
  1. I have been using blogger. com for a while now and I want to see if I can make it pay. It won't let me set up Adsense. But it's made sure that it's taken £10 of me for my domain name. It's not even working anymore ?
    Bbc78782019-12-20 02:59 Reply Mac OS X | Safari浏览器 602.1
    • Third-party blogging platforms generally cannot access Adsense. Need to be your own site, the domain name is your own.
      BCS2019-12-20 09:12 Reply Mac OS X | Chrome 79.0.3945.88