Skip to main content

Good Morning PX-sters

I would really like to add some more data to the pageview event.

I see in the inSidedData object is the module info … I would like to add this in order to segment page view analytics. 

 

If I have a module attribute, how do i get this piece of information sent with the pageview event?

 

Thanks in advance,

 

Alistair

You won’t be able to add payload to the standard PX page view event, but that’s a perfect use case for custom events.

 

You can drill down into the inSidedData object to populate custom event properties.

 

Simple Example: Community Search

aptrinsic('track','CC Search',{"query":inSidedData.search.phrase,"resultCount":inSidedData.search.count});

We pass the search query and count of results by passing the inSidedData values

 

More detailed Example: Post Like in Context

var likeButton = document.querySelector(".icon--thumb-up");

if(likeButton!=null){

likeButton.addEventListener("click", e => {

aptrinsic('track','Like Button Clicked',{'title':inSidedData.content.topic.title,'type':inSidedData.content.topic.type});

});

}

 

So you can pass a custom event triggered on a page load to include inSidedData.search.module as one of the event parameters. You’ll also be able to then groupBy that parameter in query builder to aggregate events by module.


Thanks ​@rschlette 

This also answered another question i had from my knowledge manager about search terms, so this is awesome.

I am struggling though to get the actual search phrase into a variable. 

<script>
query = inSidedData.search.phrase;
results = inSidedData.search.count;

aptrinsic('track', 'CC Search', {
"query": query,
"resultCount": results
});

console.log('search term(s) =:', query);

</script>


I added the console log to print into the screen so i can see if the variable is being picked up, which it is not. 

 

Can you help me clean it up?


Sure thing. I’m making the assumption that you want to send an event to PX when the search results page loads. If that’s not right, let me know the event trigger.

 

For search query and result count on page load, I’d use something like this:

// Check for 'search' query parameter on page load
window.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const searchParam = urlParams.get('search');

if (searchParam !== null && searchParam.trim() !== '') {
// Send search data to PX
query = inSidedData.search.phrase;
results = inSidedData.search.count;
aptrinsic('track', 'CC Search', {
"query": query,
"resultCount": results
});
// test point
console.log(results+' results for search term(s): ' + query);
}
});

So the workflow is:

  1. page load
  2. check for the presence of the ‘search’ query param (to indicate that this is a search result page)
  3. grab the search query and result count from inSidedData
  4. send the above to PX in a custom event
  5. optional console test point for easy debugging

You can refine that conditional statement all sorts of ways, but in this case the event fires anytime a page loads with an active search param value. Let me know if you run into any issues using that. My current employer isn’t a CC customer, so I don’t have a great test environment anymore, but I’m happy to help.


@rschlette 

I have copied and pasted the code above into INSERT BEFORE </BODY> TAG within Control.

I do not get any event in PX, nor does the test point.

as you can see here it is in the source code.

the custom event is not being sent, the console is not printing. any thoughts?

 

Thanks in advance


I see one thing. In the line where we define searchParam, I was using the parameter name ‘search’ when it appears that the live CC site uses ‘q’

 

so the line that reads:

const searchParam = urlParams.get('search');

should read:

const searchParam = urlParams.get('q');

That one works using Tamper Monkey on this site, so hopefully it will work on your site as well.


One additional note, I’m not sure on that site whether you can have the script load at the end of the body element and use the DOMContentLoaded event effectively. You might try moving this script to the top of the body element so that the event listener is loaded before the DOMContentLoaded event fires.