3

I'm trying to set Analytics Tracking Event as Download using Sitecore PowerShell script for most media items which have pdf extension.

like this:

enter image description here

Below is the script, I have written but not working

$items = Get-ChildItem -Path "master:/sitecore/media library/Files/Documentation Center/Reports/Reporting Brochure" -Recurse

foreach($item in $items){
 $fieldValue = $item."Extension"
if(-Not([string]::IsNullOrEmpty($fieldValue)) -and $item."Extension" -eq "pdf" )
{

    $item.Editing.BeginEdit();
    $item.Fields["__Tracking"].Value = "<tracking><event id='{FA72E131-3CFD-481C-8E15-04496E9586DC}' name='Download' /></tracking>"
    $item.Editing.EndEdit();
 
}
}

2 Answers 2

1

I've tried your script directly under the files folder with pdf files and it worked for me.

enter image description here

enter image description here

Few thinks you can try out.

  1. Validate the extention of the file.
  2. Put pdf file directly under file folder for testing and see if it works.

You can check the extension of the files you are trying for. Your screenshot does not show like pdf file. Validate the information section of the file.

enter image description here

$items = Get-ChildItem -Path "master:/sitecore/media library/Files" -Recurse

foreach($item in $items){
 $fieldValue = $item."Extension"
if(-Not([string]::IsNullOrEmpty($fieldValue)) -and $item."Extension" -eq "pdf" )
{

    $item.Editing.BeginEdit();
    $item.Fields["__Tracking"].Value = "<tracking><event id='{FA72E131-3CFD-481C-8E15-04496E9586DC}' name='Download' /></tracking>"
    $item.Editing.EndEdit();
 
}
}

Hope it helps!

2
  • Thanks, Raman now the script is running my end also
    – md shahbaj
    Commented Mar 24, 2023 at 4:44
  • I'm glad it helps you. If any of the answer works for you than you can mark it as accepted. Refer sitecore.stackexchange.com/tour Commented Mar 24, 2023 at 5:22
0

Yes, the script you have written is working and I am also able to set the tracking value.

enter image description here

You should debug your script to check the following.

  1. Are you able to get the records in the variable $items
  2. Use to write something in the console like below to see if your condition is getting fulfilled like below.

And check what items do not fulfill this condition by getting their ID in the console in the else part.

if(-Not([string]::IsNullOrEmpty($fieldValue)) -and $item."Extension" -eq "pdf" )
{
    Write-Host 'Item Found'
    $item.Editing.BeginEdit();
    $item.Fields["__Tracking"].Value = "<tracking><event id='{FA72E131-3CFD-481C-8E15-04496E9586DC}' name='Download' /></tracking>"
    $item.Editing.EndEdit();
 
}
else
{
    Write-Host 'Not Found'
    Write-Host $item.Id
}  

Because the script you are using only does a normal Item edit operation and it may fail in the conditions you have written before doing this event.

  1. Also, check if you manually update the value of Tracking field in one of the PDF items in your folder by viewing the raw values of the item and updating and saving it.

Hope this will give you an idea of what items it is failing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.